diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-05-01 21:13:32 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-05-03 21:23:16 +0300 |
commit | 24ec67c9f6375d34d88e79981ed8abbe15a78169 (patch) | |
tree | 6e68b1b409b10b48581422df05cfac9caedb0af2 /src/grecs-lex.l | |
parent | da56e338d346e358de864d18ca42f574305d8dfc (diff) | |
download | grecs-24ec67c9f6375d34d88e79981ed8abbe15a78169.tar.gz grecs-24ec67c9f6375d34d88e79981ed8abbe15a78169.tar.bz2 |
Reindent all, except wordsplit, which is shared with MU.
Diffstat (limited to 'src/grecs-lex.l')
-rw-r--r-- | src/grecs-lex.l | 579 |
1 files changed, 276 insertions, 303 deletions
diff --git a/src/grecs-lex.l b/src/grecs-lex.l index baf85de..9c01e0d 100644 --- a/src/grecs-lex.l +++ b/src/grecs-lex.l @@ -34,7 +34,7 @@ static char *multiline_delimiter; static size_t multiline_delimiter_len; static int multiline_unescape; /* Unescape here-document contents */ -static int (*char_to_strip) (char); /* Strip matching characters of each +static int (*char_to_strip)(char); /* Strip matching characters of each here-document line */ grecs_locus_t grecs_current_locus; /* Input file location */ @@ -50,26 +50,26 @@ static size_t xlines; static struct grecs_list *line_acc; static struct grecs_list *string_list; -static void multiline_begin (char *); -static void multiline_add (char *); -static char *multiline_strip_tabs (char *text); -static void line_add_unescape_last (char *text, size_t len); -static int ident (void); -static int isemptystr (int off); +static void multiline_begin(char *); +static void multiline_add(char *); +static char *multiline_strip_tabs(char *text); +static void line_add_unescape_last(char *text, size_t len); +static int ident(void); +static int isemptystr(int off); -static void parse_line (char *text, grecs_locus_t *ploc, size_t *pxlines); -static void parse_line_cpp (char *text, grecs_locus_t *ploc, size_t *pxlines); +static void parse_line(char *text, grecs_locus_t *ploc, size_t *pxlines); +static void parse_line_cpp(char *text, grecs_locus_t *ploc, size_t *pxlines); #undef YY_INPUT #define YY_INPUT(buf,result,max_size) \ do \ { \ if (grecs_preprocessor) \ - result = fread (buf, 1, max_size, yyin); \ + result = fread(buf, 1, max_size, yyin); \ else \ result = grecs_preproc_fill_buffer(buf, max_size); \ } \ - while (0) + while(0) %} @@ -82,16 +82,16 @@ P [1-9][0-9]* %% /* C-style comments */ -"/*" BEGIN (COMMENT); +"/*" BEGIN(COMMENT); <COMMENT>[^*\n]* /* eat anything that's not a '*' */ <COMMENT>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */ <COMMENT>\n ++grecs_current_locus.line; -<COMMENT>"*"+"/" BEGIN (INITIAL); +<COMMENT>"*"+"/" BEGIN(INITIAL); /* Line directive */ -^[ \t]*#[ \t]*{P}[ \t]+\".*\".*\n { parse_line_cpp (yytext, +^[ \t]*#[ \t]*{P}[ \t]+\".*\".*\n { parse_line_cpp(yytext, &grecs_current_locus, &xlines); } -^[ \t]*#[ \t]*line[ \t].*\n { parse_line (yytext, &grecs_current_locus, +^[ \t]*#[ \t]*line[ \t].*\n { parse_line(yytext, &grecs_current_locus, &xlines); } /* End-of-line comments */ #.*\n { grecs_current_locus.line++; } @@ -99,27 +99,27 @@ P [1-9][0-9]* "//".*\n { grecs_current_locus.line++; } "//".* /* end-of-file comment */; /* Identifiers */ -<INITIAL>{ID} return ident (); +<INITIAL>{ID} return ident(); /* Strings */ -[a-zA-Z0-9_\.\*/:@-]+ { grecs_line_begin (); - grecs_line_add (yytext, yyleng); - yylval.string = grecs_line_finish (); - return STRING; } +[a-zA-Z0-9_\.\*/:@-]+ { grecs_line_begin(); + grecs_line_add(yytext, yyleng); + yylval.string = grecs_line_finish(); + return STRING; } /* Quoted strings */ -\"[^\\"\n]*\" { grecs_line_begin (); - grecs_line_add (yytext + 1, yyleng - 2); - yylval.string = grecs_line_finish (); +\"[^\\"\n]*\" { grecs_line_begin(); + grecs_line_add(yytext + 1, yyleng - 2); + yylval.string = grecs_line_finish(); return QSTRING; } \"[^\\"\n]*\\. | -\"[^\\"\n]*\\\n { BEGIN (STR); - grecs_line_begin (); - line_add_unescape_last (yytext + 1, yyleng - 1); } +\"[^\\"\n]*\\\n { BEGIN(STR); + grecs_line_begin(); + line_add_unescape_last(yytext + 1, yyleng - 1); } <STR>[^\\"\n]*\\. | -<STR>\"[^\\"\n]*\\\n { line_add_unescape_last (yytext, yyleng); } +<STR>\"[^\\"\n]*\\\n { line_add_unescape_last(yytext, yyleng); } <STR>[^\\"\n]*\" { BEGIN(INITIAL); if (yyleng > 1) - grecs_line_add (yytext, yyleng - 1); - yylval.string = grecs_line_finish (); + grecs_line_add(yytext, yyleng - 1); + yylval.string = grecs_line_finish(); return QSTRING; } /* Multiline strings */ "<<"(-" "?)?\\?{ID}[ \t]*#.*\n | @@ -128,402 +128,375 @@ P [1-9][0-9]* "<<"(-" "?)?\"{ID}\"[ \t]*#.*\n | "<<"(-" "?)?\"{ID}\"[ \t]*"//".*\n | "<<"(-" "?)?\"{ID}\"[ \t]*\n { - BEGIN (ML); - multiline_begin (yytext+2); + BEGIN(ML); + multiline_begin(yytext+2); grecs_current_locus.line++; } /* Ignore m4 line statements */ <ML>^"#line ".*\n { grecs_current_locus.line++; } -<ML>.*\n { char *p = multiline_strip_tabs (yytext); +<ML>.*\n { char *p = multiline_strip_tabs(yytext); - if (!strncmp (p, multiline_delimiter, multiline_delimiter_len) - && isemptystr (p + multiline_delimiter_len - yytext)) - { - free (multiline_delimiter); - multiline_delimiter = NULL; - BEGIN (INITIAL); - yylval.string = grecs_line_finish (); - return MSTRING; - } + if (!strncmp(p, multiline_delimiter, multiline_delimiter_len) + && isemptystr(p + multiline_delimiter_len - yytext)) { + free(multiline_delimiter); + multiline_delimiter = NULL; + BEGIN(INITIAL); + yylval.string = grecs_line_finish(); + return MSTRING; + } grecs_current_locus.line++; - multiline_add (p); } + multiline_add(p); } {WS} ; /* Other tokens */ \n { grecs_current_locus.line++; } [,;{}()] return yytext[0]; -. { if (isascii (yytext[0]) && isprint (yytext[0])) - grecs_error (&grecs_current_locus, 0, _("stray character %c"), yytext[0]); +. { if (isascii(yytext[0]) && isprint(yytext[0])) + grecs_error(&grecs_current_locus, 0, + _("stray character %c"), yytext[0]); else - grecs_error (&grecs_current_locus, 0, _("stray character \\%03o"), + grecs_error(&grecs_current_locus, 0, _("stray character \\%03o"), (unsigned char) yytext[0]); } %% pid_t grecs_preproc_pid; int -yywrap () +yywrap() { - if (yyin) - grecs_preproc_extrn_shutdown (grecs_preproc_pid); - else - grecs_preproc_done (); - grecs_current_locus.file = NULL; - return 1; + if (yyin) + grecs_preproc_extrn_shutdown(grecs_preproc_pid); + else + grecs_preproc_done(); + grecs_current_locus.file = NULL; + return 1; } static void -line_acc_free_entry (void *ptr) +line_acc_free_entry(void *ptr) { - free (ptr); + free(ptr); } int -grecs_lex_begin (const char *name) +grecs_lex_begin(const char *name) { - if (yy_flex_debug > 0) - yy_flex_debug = 0; + if (yy_flex_debug > 0) + yy_flex_debug = 0; - line_acc = grecs_list_create (); - line_acc->free_entry = line_acc_free_entry; - string_list = grecs_list_create (); - - if (grecs_preprocessor) - { - int fd; + line_acc = grecs_list_create(); + line_acc->free_entry = line_acc_free_entry; + string_list = grecs_list_create(); - fd = open (name, O_RDONLY); - if (fd == -1) - { - grecs_error (NULL, errno, _("Cannot open `%s'"), name); - return 1; - } - close (fd); - - yyin = grecs_preproc_extrn_start (name, &grecs_preproc_pid); - if (!yyin) - { - grecs_error (NULL, errno, - _("Unable to start external preprocessor `%s'"), - grecs_preprocessor); - return 1; - } - } - else - return grecs_preproc_init (name); - - return 0; + if (grecs_preprocessor) { + int fd; + + fd = open(name, O_RDONLY); + if (fd == -1) { + grecs_error(NULL, errno, _("Cannot open `%s'"), name); + return 1; + } + close(fd); + + yyin = grecs_preproc_extrn_start(name, &grecs_preproc_pid); + if (!yyin) { + grecs_error(NULL, errno, + _("Unable to start external preprocessor `%s'"), + grecs_preprocessor); + return 1; + } + } else + return grecs_preproc_init(name); + + return 0; } void -grecs_lex_end () +grecs_lex_end() { } static int -isemptystr (int off) +isemptystr(int off) { - for (; yytext[off] && isspace (yytext[off]); off++) - ; - if (yytext[off] == ';') - { - int i; - for (i = off + 1; yytext[i]; i++) - if (!isspace (yytext[i])) - return 0; - yyless (off); - return 1; - } - return yytext[off] == 0; + for (; yytext[off] && isspace(yytext[off]); off++) + ; + if (yytext[off] == ';') { + int i; + for (i = off + 1; yytext[i]; i++) + if (!isspace(yytext[i])) + return 0; + yyless(off); + return 1; + } + return yytext[off] == 0; } char * -multiline_strip_tabs (char *text) +multiline_strip_tabs(char *text) { - if (char_to_strip) - for (; *text && char_to_strip (*text); text++) - ; - return text; + if (char_to_strip) + for (; *text && char_to_strip(*text); text++) + ; + return text; } static int -unquote_char (int c) +unquote_char(int c) { - static char quote_transtab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v"; - - char *p; - - for (p = quote_transtab; *p; p += 2) - { - if (*p == c) - return p[1]; - } - return -1; + static char quote_transtab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v"; + + char *p; + + for (p = quote_transtab; *p; p += 2) { + if (*p == c) + return p[1]; + } + return -1; } struct line_acc_entry { - size_t size; + size_t size; }; #define line_acc_ptr(entry) (char*)(entry + 1) static void -line_acc_add_string (const char *str, size_t len) +line_acc_add_string(const char *str, size_t len) { - struct line_acc_entry *ent = grecs_malloc (sizeof (*ent) + len + 1); - char *p = line_acc_ptr (ent); - memcpy (p, str, len); - p[len] = 0; - ent->size = len; - grecs_list_append (line_acc, ent); + struct line_acc_entry *ent = grecs_malloc(sizeof(*ent) + len + 1); + char *p = line_acc_ptr(ent); + memcpy(p, str, len); + p[len] = 0; + ent->size = len; + grecs_list_append(line_acc, ent); } static void -line_acc_add_char (int c) +line_acc_add_char(int c) { - char t = c; - line_acc_add_string (&t, 1); + char t = c; + line_acc_add_string(&t, 1); } static void -list_acc_unescape_char (int c) +list_acc_unescape_char(int c) { - if (c != '\n') - { - int t = unquote_char (c); - if (t != -1) - line_acc_add_char (t); - else - { - grecs_warning(&grecs_current_locus, 0, - _("unknown escape sequence '\\%c'"), - c); - line_acc_add_char (c); + if (c != '\n') { + int t = unquote_char(c); + if (t != -1) + line_acc_add_char(t); + else { + grecs_warning(&grecs_current_locus, 0, + _("unknown escape sequence '\\%c'"), + c); + line_acc_add_char(c); + } } - } } void -grecs_line_add (const char *text, size_t len) +grecs_line_add(const char *text, size_t len) { - line_acc_add_string (text, len); + line_acc_add_string(text, len); } /* Same, but unescapes the last character from yytext */ static void -line_add_unescape_last (char *text, size_t len) +line_add_unescape_last(char *text, size_t len) { - line_acc_add_string (text, len - 2); - list_acc_unescape_char (text[len - 1]); + line_acc_add_string(text, len - 2); + list_acc_unescape_char(text[len - 1]); } static void -multiline_add (char *s) +multiline_add(char *s) { - if (multiline_unescape) - { - for (; *s; s++) - { - if (*s == '\\') - { - list_acc_unescape_char (s[1]); - ++s; - } - else - line_acc_add_char (*s); - } - } - else - grecs_line_add (s, strlen (s)); + if (multiline_unescape) { + for (; *s; s++) { + if (*s == '\\') { + list_acc_unescape_char(s[1]); + ++s; + } else + line_acc_add_char(*s); + } + } else + grecs_line_add(s, strlen(s)); } void -grecs_line_begin () +grecs_line_begin() { /* FIXME: nothing so far. Maybe prepare stk by calling obstack_finish? */ } static int -is_tab (char c) +is_tab(char c) { - return c == '\t'; + return c == '\t'; } static int -is_ws (char c) +is_ws(char c) { - return c == '\t' || c == ' '; + return c == '\t' || c == ' '; } void -multiline_begin (char *p) +multiline_begin(char *p) { - if (*p == '-') - { - if (*++p == ' ') - { - char_to_strip = is_ws; - p++; - } - else - char_to_strip = is_tab; - } - else - char_to_strip = NULL; - if (*p == '\\') - { - p++; - multiline_unescape = 0; - } - else if (*p == '"') - { - char *q; + if (*p == '-') { + if (*++p == ' ') { + char_to_strip = is_ws; + p++; + } else + char_to_strip = is_tab; + } else + char_to_strip = NULL; + if (*p == '\\') { + p++; + multiline_unescape = 0; + } else if (*p == '"') { + char *q; - p++; - multiline_unescape = 0; - q = strchr (p, '"'); - multiline_delimiter_len = q - p; - } - else - { - multiline_delimiter_len = strcspn (p, " \t"); - multiline_unescape = 1; - } - - /* Remove trailing newline */ - multiline_delimiter_len--; - multiline_delimiter = grecs_malloc (multiline_delimiter_len + 1); - memcpy (multiline_delimiter, p, multiline_delimiter_len); - multiline_delimiter[multiline_delimiter_len] = 0; - grecs_line_begin (); + p++; + multiline_unescape = 0; + q = strchr(p, '"'); + multiline_delimiter_len = q - p; + } else { + multiline_delimiter_len = strcspn(p, " \t"); + multiline_unescape = 1; + } + + /* Remove trailing newline */ + multiline_delimiter_len--; + multiline_delimiter = grecs_malloc(multiline_delimiter_len + 1); + memcpy(multiline_delimiter, p, multiline_delimiter_len); + multiline_delimiter[multiline_delimiter_len] = 0; + grecs_line_begin(); } char * -grecs_line_finish () +grecs_line_finish() { - struct grecs_list_entry *ep; - size_t size = 0; - char *str, *p; - - for (ep = line_acc->head; ep; ep = ep->next) - { - struct line_acc_entry *ent = ep->data; - size += ent->size; - } - - str = grecs_malloc (size + 1); - grecs_list_append (string_list, str); - for (ep = line_acc->head, p = str; ep; ep = ep->next) - { - struct line_acc_entry *ent = ep->data; - char *str = line_acc_ptr (ent); - memcpy (p, str, ent->size); - p += ent->size; - } - *p = 0; - grecs_list_clear (line_acc); - return str; + struct grecs_list_entry *ep; + size_t size = 0; + char *str, *p; + + for (ep = line_acc->head; ep; ep = ep->next) { + struct line_acc_entry *ent = ep->data; + size += ent->size; + } + + str = grecs_malloc(size + 1); + grecs_list_append(string_list, str); + for (ep = line_acc->head, p = str; ep; ep = ep->next) { + struct line_acc_entry *ent = ep->data; + char *str = line_acc_ptr(ent); + memcpy(p, str, ent->size); + p += ent->size; + } + *p = 0; + grecs_list_clear(line_acc); + return str; } static int -ident () +ident() { - char *p; - char *str; - size_t len; + char *p; + char *str; + size_t len; - for (p = yytext; *p && isspace (*p); p++) - ; - - len = strlen (p); - str = grecs_malloc (len + 1); - strcpy (str, p); - grecs_list_append (string_list, str); - yylval.string = str; - return IDENT; + for (p = yytext; *p && isspace(*p); p++) + ; + + len = strlen(p); + str = grecs_malloc(len + 1); + strcpy(str, p); + grecs_list_append(string_list, str); + yylval.string = str; + return IDENT; } void -grecs_lex_trace (int n) +grecs_lex_trace(int n) { - yy_flex_debug = -n; + yy_flex_debug = -n; } grecs_value_t * -grecs_value_dup (grecs_value_t *input) +grecs_value_dup(grecs_value_t *input) { - grecs_value_t *ptr = grecs_malloc (sizeof (*ptr)); - grecs_list_append (string_list, ptr); - *ptr = *input; - return ptr; + grecs_value_t *ptr = grecs_malloc(sizeof(*ptr)); + grecs_list_append(string_list, ptr); + *ptr = *input; + return ptr; } static int -assign_locus (grecs_locus_t *ploc, char *name, char *line, size_t *pxlines) +assign_locus(grecs_locus_t *ploc, char *name, char *line, size_t *pxlines) { - char *p; - - if (name) - { - if (pxlines && (!ploc->file || strcmp(name, ploc->file))) - *pxlines = 0; - ploc->file = grecs_install_text (name); - } - ploc->line = strtoul (line, &p, 10) - (pxlines ? *pxlines : 0); - return *p != 0; + char *p; + + if (name) { + if (pxlines && (!ploc->file || strcmp(name, ploc->file))) + *pxlines = 0; + ploc->file = grecs_install_text(name); + } + ploc->line = strtoul(line, &p, 10) - (pxlines ? *pxlines : 0); + return *p != 0; } static void -parse_line (char *text, grecs_locus_t *ploc, size_t *pxlines) +parse_line(char *text, grecs_locus_t *ploc, size_t *pxlines) { - int rc = 1; - struct wordsplit ws; - - if (wordsplit (text, &ws, WRDSF_DEFFLAGS)) - grecs_error (ploc, 0, _("cannot parse #line line")); - else - { - if (ws.ws_wordc == 2) - rc = assign_locus (ploc, NULL, ws.ws_wordv[1], pxlines); - else if (ws.ws_wordc == 3) - rc = assign_locus (ploc, ws.ws_wordv[2], ws.ws_wordv[1], pxlines); - else if (ws.ws_wordc == 4) - { - rc = assign_locus (ploc, ws.ws_wordv[2], ws.ws_wordv[1], 0); - if (rc == 0) - { - char *p; - unsigned long x = strtoul (ws.ws_wordv[3], &p, 10); - rc = *p != 0; - if (rc == 0) - *pxlines = x; - } - } - else - grecs_error (ploc, 0, _("invalid #line statement")); + int rc = 1; + struct wordsplit ws; + + if (wordsplit(text, &ws, WRDSF_DEFFLAGS)) + grecs_error(ploc, 0, _("cannot parse #line line")); + else { + if (ws.ws_wordc == 2) + rc = assign_locus(ploc, NULL, + ws.ws_wordv[1], pxlines); + else if (ws.ws_wordc == 3) + rc = assign_locus(ploc, ws.ws_wordv[2], + ws.ws_wordv[1], pxlines); + else if (ws.ws_wordc == 4) { + rc = assign_locus(ploc, ws.ws_wordv[2], + ws.ws_wordv[1], 0); + if (rc == 0) { + char *p; + unsigned long x = strtoul(ws.ws_wordv[3], + &p, 10); + rc = *p != 0; + if (rc == 0) + *pxlines = x; + } + } else + grecs_error(ploc, 0, _("invalid #line statement")); - if (rc) - grecs_error (ploc, 0, _("malformed #line statement")); - wordsplit_free (&ws); - } + if (rc) + grecs_error(ploc, 0, _("malformed #line statement")); + wordsplit_free(&ws); + } } static void -parse_line_cpp (char *text, grecs_locus_t *ploc, size_t *pxlines) +parse_line_cpp(char *text, grecs_locus_t *ploc, size_t *pxlines) { - struct wordsplit ws; - - if (wordsplit (text, &ws, WRDSF_DEFFLAGS)) - { - grecs_error (ploc, 0, _("cannot parse #line line")); - return; - } - else if (ws.ws_wordc < 3) - grecs_error (ploc, 0, _("invalid #line statement")); - else - { - if (assign_locus (ploc, ws.ws_wordv[2], ws.ws_wordv[1], pxlines)) - grecs_error (ploc, 0, _("malformed #line statement")); - } - wordsplit_free (&ws); + struct wordsplit ws; + + if (wordsplit(text, &ws, WRDSF_DEFFLAGS)) { + grecs_error(ploc, 0, _("cannot parse #line line")); + return; + } else if (ws.ws_wordc < 3) + grecs_error(ploc, 0, _("invalid #line statement")); + else { + if (assign_locus(ploc, ws.ws_wordv[2], + ws.ws_wordv[1], pxlines)) + grecs_error(ploc, 0, _("malformed #line statement")); + } + wordsplit_free(&ws); } |