summaryrefslogtreecommitdiff
path: root/yacc
diff options
context:
space:
mode:
authorDemi Obenour <demiobenour@gmail.com>2017-01-15 14:42:19 -0500
committerDavid Allsopp <david.allsopp@metastack.com>2017-05-17 09:48:41 +0100
commit795f1bb37a5a1c83cea2474a4608e0b0e53a4a64 (patch)
tree2acc7d270105e00a5d94bd4c2a4138edf5313d76 /yacc
parentaf5503722990acbb1f161eea25e1934581e1650b (diff)
downloadocaml-795f1bb37a5a1c83cea2474a4608e0b0e53a4a64.tar.gz
Fix the parsing of comments by ocamlyacc
Diffstat (limited to 'yacc')
-rw-r--r--yacc/reader.c107
1 files changed, 67 insertions, 40 deletions
diff --git a/yacc/reader.c b/yacc/reader.c
index 017f64c6f9..58d4c3514b 100644
--- a/yacc/reader.c
+++ b/yacc/reader.c
@@ -64,19 +64,23 @@ static char *buffer;
static size_t length;
static size_t capacity;
static void push_stack(char x) {
- if (length - 1 >= capacity) {
- buffer = realloc(buffer, capacity = 3*length/2 + 100);
- if (!buffer) no_space();
- }
- buffer[++length] = x;
- buffer[0] = '\1';
+ if (length - 1 >= capacity) {
+ buffer = realloc(buffer, capacity = 3*length/2 + 100);
+ if (!buffer) no_space();
+ }
+ buffer[++length] = x;
+ buffer[0] = '\1';
}
static void pop_stack(char x) {
- if (!buffer || buffer[length--] != x) {
- char newx = x == '(' ? ')' : x == '{' ? '}' : x;
- fprintf(stderr, "Mismatched parentheses or braces: '%c'\n", newx);
- syntax_error(lineno, line, cptr - 1);
+ if (!buffer || buffer[length--] != x) {
+ switch (x) {
+ case '{': x = '}'; break;
+ case '(': x = ')'; break;
+ default: break;
+ }
+ fprintf(stderr, "Mismatched parentheses or braces: '%c'\n", x);
+ syntax_error(lineno, line, cptr - 1);
}
}
@@ -236,35 +240,6 @@ void process_apostrophe(FILE *const f)
}
}
-static void process_comment(FILE *const f) {
- char c = *cptr;
- if (c == '*')
- {
- int c_lineno = lineno;
- char *c_line = dup_line();
- char *c_cptr = c_line + (cptr - line - 1);
-
- putc('*', f);
- ++cptr;
- for (;;)
- {
- c = *cptr++;
- putc(c, f);
- if (c == '*' && *cptr == ')')
- {
- FREE(c_line);
- return;
- }
- if (c == '\n')
- {
- get_line();
- if (line == 0)
- unterminated_comment(c_lineno, c_line, c_cptr);
- }
- }
- }
-}
-
static void process_open_curly_bracket(FILE *f) {
if (In_bitmap(caml_ident_start, *cptr) || *cptr == '|')
{
@@ -327,6 +302,58 @@ static void process_open_curly_bracket(FILE *f) {
return;
}
+static void process_comment(FILE *const f) {
+ char c = *cptr;
+ unsigned depth = 1;
+ if (c == '*')
+ {
+ int c_lineno = lineno;
+ char *c_line = dup_line();
+ char *c_cptr = c_line + (cptr - line - 1);
+
+ putc('*', f);
+ ++cptr;
+ for (;;)
+ {
+ c = *cptr++;
+ putc(c, f);
+
+ switch (c)
+ {
+ case '*':
+ if (*cptr == ')')
+ {
+ depth--;
+ if (depth == 0) {
+ FREE(c_line);
+ return;
+ }
+ }
+ continue;
+ case '\n':
+ get_line();
+ if (line == 0)
+ unterminated_comment(c_lineno, c_line, c_cptr);
+ continue;
+ case '(':
+ if (*cptr == '*') ++depth;
+ continue;
+ case '\'':
+ process_apostrophe(f);
+ continue;
+ case '"':
+ process_quoted_string(c, f);
+ continue;
+ case '{':
+ process_open_curly_bracket(f);
+ continue;
+ default:
+ continue;
+ }
+ }
+ }
+}
+
char *substring (char *str, int start, int len)
{
int i;
@@ -1311,8 +1338,8 @@ loop:
goto loop;
case '(':
- process_comment(f);
push_stack('(');
+ process_comment(f);
goto loop;
case ')':