int lexer_unquoted_look_ahead_count(struct lexer_book *bk, char *char_set) { char c = -1; int count = 0; int double_quote = 0; int single_quote = 0; do { c = lexer_next_char(bk); count++; if( double_quote || single_quote ) { if( c == '"' && double_quote ) double_quote = 0; else if ( c == '\'' && single_quote ) single_quote = 0; } else if( strchr(char_set, c) ) { break; } else if( c == '\\' ) { lexer_next_char(bk); count++; } else if( c == '"' ) { double_quote = 1; } else if( c == '\'' ) { single_quote = 1; } } while( c != '\n' && c != CHAR_EOF ); lexer_roll_back(bk, count); if( c == CHAR_EOF ) { if( strchr(char_set, CHAR_EOF) ) return count; else return -1; } else if( c == '\n' ) { if( strchr(char_set, '\n') ) return count; else return -1; } else return count; }
accept_t lexer_read_variable(struct lexer_book * bk, struct token * name) { lexer_discard_white_space(bk); if(lexer_next_peek(bk) == '=') { lexer_next_char(bk); lexer_add_to_lexeme(bk, '='); } else { int c = lexer_next_char(bk); if(lexer_next_peek(bk) != '=') return NO; lexer_add_to_lexeme(bk, c); lexer_next_char(bk); /* Jump = */ } lexer_push_token(bk, lexer_pack_token(bk, VARIABLE)); lexer_push_token(bk, name); lexer_discard_white_space(bk); lexer_read_expandable(bk, '\n'); lexer_roll_back(bk, 1); //Recover '\n' lexer_discard_white_space(bk); if(lexer_next_char(bk) != '\n') return NO; return YES; }
int lexer_read_variable(struct lexer *lx, struct token *name) { lexer_discard_white_space(lx); if(lexer_next_peek(lx) == '=') { lexer_next_char(lx); lexer_add_to_lexeme(lx, '='); } else { int c = lexer_next_char(lx); if(lexer_next_peek(lx) != '=') lexer_report_error(lx, "Missing = in variable definition."); lexer_add_to_lexeme(lx, c); lexer_next_char(lx); /* Jump = */ } lexer_push_token(lx, lexer_pack_token(lx, TOKEN_VARIABLE)); lexer_push_token(lx, name); lexer_discard_white_space(lx); //Read variable value lexer_push_token(lx, lexer_read_expandable(lx, '\n')); lexer_roll_back(lx, 1); //Recover '\n' lexer_discard_white_space(lx); if(lexer_next_char(lx) != '\n') lexer_report_error(lx, "Missing newline at end of variable definition."); return 1; }
int lexer_next_peek(struct lexer_book *bk) { /* Read next chunk if necessary */ int c = lexer_next_char(bk); lexer_roll_back(bk, 1); return c; }
int lexer_next_peek(struct lexer *lx) { /* Read next chunk if necessary */ int c = lexer_next_char(lx); lexer_roll_back(lx, 1); return c; }
int lexer_peek_remote_rename_syntax(struct lexer_book *bk) { if(lexer_next_peek(bk) != '-') return 0; lexer_next_char(bk); int is_gt = (lexer_next_peek(bk) == '>'); lexer_roll_back(bk, 1); return is_gt; }
int lexer_peek_remote_rename_syntax(struct lexer *lx) { if(lexer_next_peek(lx) != '-') return 0; lexer_next_char(lx); int is_gt = (lexer_next_peek(lx) == '>'); lexer_roll_back(lx, 1); return is_gt; }
accept_t lexer_read_syntax(struct lexer_book * bk) { lexer_discard_white_space(bk); struct token *name = lexer_read_syntax_name(bk); if(strcmp("export", name->lexeme) == 0) return lexer_read_syntax_export(bk, name); else if(lexer_unquoted_look_ahead_count(bk, "=") > -1) return lexer_read_variable(bk, name); else { lexer_roll_back(bk, strlen(name->lexeme)); lexer_report_error(bk, "Unrecognized keyword: %s.", name->lexeme); } return NO; }
int lexer_read_syntax_or_variable(struct lexer * lx) { lexer_discard_white_space(lx); struct token *name = lexer_read_syntax_name(lx); if(strcmp("export", name->lexeme) == 0) return lexer_read_syntax_export(lx, name); else if(lexer_unquoted_look_ahead_count(lx, "=") > -1) return lexer_read_variable(lx, name); else { lexer_roll_back(lx, strlen(name->lexeme)); lexer_report_error(lx, "Unrecognized keyword: %s.", name->lexeme); } return 1; }
int lexer_read_variable_list(struct lexer * lx) { int c; while((c = lexer_next_peek(lx)) != '\n') { lexer_discard_white_space(lx); if(c == '#') { lexer_discard_comments(lx); lexer_roll_back(lx, 1); //Recover the newline break; } lexer_push_token(lx, lexer_read_syntax_name(lx)); } lexer_add_to_lexeme(lx, lexer_next_char(lx)); //Drop the newline lexer_push_token(lx, lexer_pack_token(lx, TOKEN_NEWLINE)); return 1; }
accept_t lexer_read_variable_list(struct lexer_book * bk) { int c; while((c = lexer_next_peek(bk)) != '\n') { lexer_discard_white_space(bk); if(c == '#') { lexer_discard_comments(bk); lexer_roll_back(bk, 1); //Recover the newline break; } lexer_push_token(bk, lexer_read_syntax_name(bk)); } lexer_add_to_lexeme(bk, lexer_next_char(bk)); //Drop the newline lexer_push_token(bk, lexer_pack_token(bk, NEWLINE)); return YES; }