crowbar.l(Ver.0.4.02)

  1: %{
  2: #undef YY_INPUT
  3: #define YY_INPUT(buf, result, max_size) (result = my_yyinput(buf, max_size))
  4: #include <stdio.h>
  5: #include <string.h>
  6: #include "DBG.h"
  7: #include "crowbar.h"
  8: #include "y.tab.h"
  9: 
 10: static int
 11: file_input(char *buf, int max_size)
 12: {
 13:     int ch;
 14:     int len;
 15: 
 16:     if (feof(yyin))
 17:         return 0;
 18: 
 19:     for (len = 0; len < max_size; len++) {
 20:         ch = getc(yyin);
 21:         if (ch == EOF)
 22:             break;
 23:         buf[len] = ch;
 24:     }
 25:     return len;
 26: }
 27: 
 28: static char **st_source_string;
 29: static int st_current_source_line;
 30: static int st_current_char_index;
 31:  
 32: void
 33: crb_set_source_string(char **source)
 34: {
 35:     st_source_string = source;
 36:     st_current_source_line = 0;
 37:     st_current_char_index = 0;
 38: }
 39: 
 40: static int
 41: string_input(char *buf, int max_size)
 42: {
 43:     int len;
 44: 
 45:     if (st_source_string[st_current_source_line] == NULL)
 46:         return 0;
 47: 
 48:     if (st_source_string[st_current_source_line][st_current_char_index]
 49:         == '\0') {
 50:         st_current_source_line++;
 51:         st_current_char_index = 0;
 52:     }
 53: 
 54:     if (st_source_string[st_current_source_line] == NULL)
 55:         return 0;
 56: 
 57:     len = smaller(strlen(st_source_string[st_current_source_line])
 58:                   - st_current_char_index,
 59:                   max_size);
 60:     strncpy(buf,
 61:             &st_source_string[st_current_source_line][st_current_char_index],
 62:             len);
 63:     st_current_char_index += len;
 64: 
 65:     return len;
 66: }
 67: 
 68: static int
 69: my_yyinput(char *buf, int max_size)
 70: {
 71:     int result;
 72: 
 73:     switch (crb_get_current_interpreter()->input_mode) {
 74:     case CRB_FILE_INPUT_MODE:
 75:         result = file_input(buf, max_size);
 76:         break;
 77:     case CRB_STRING_INPUT_MODE:
 78:         result = string_input(buf, max_size);
 79:         break;
 80:     default:
 81:         DBG_panic(("bad default. input_mode..%d\n",
 82:                    crb_get_current_interpreter()->input_mode));
 83:     }
 84: 
 85:     return result;
 86: }
 87: 
 88: 
 89: int
 90: yywrap(void)
 91: {
 92:     return 1;
 93: }
 94: 
 95: static void
 96: increment_line_number(void)
 97: {
 98:     crb_get_current_interpreter()->current_line_number++;
 99: }
100: %}
101: %start COMMENT STRING_LITERAL_STATE REGEXP_LITERAL_STATE
102: %%
103: <INITIAL>"function"     return FUNCTION;
104: <INITIAL>"if"           return IF;
105: <INITIAL>"else"         return ELSE;
106: <INITIAL>"elsif"        return ELSIF;
107: <INITIAL>"while"        return WHILE;
108: <INITIAL>"for"          return FOR;
109: <INITIAL>"foreach"      return FOREACH;
110: <INITIAL>"return"       return RETURN_T;
111: <INITIAL>"break"        return BREAK;
112: <INITIAL>"continue"     return CONTINUE;
113: <INITIAL>"null"         return NULL_T;
114: <INITIAL>"true"         return TRUE_T;
115: <INITIAL>"false"        return FALSE_T;
116: <INITIAL>"closure"      return CLOSURE;
117: <INITIAL>"global"       return GLOBAL_T;
118: <INITIAL>"try"          return TRY;
119: <INITIAL>"catch"        return CATCH;
120: <INITIAL>"finally"      return FINALLY;
121: <INITIAL>"throw"        return THROW;
122: <INITIAL>"final"        return FINAL;
123: <INITIAL>"("            return LP;
124: <INITIAL>")"            return RP;
125: <INITIAL>"{"            return LC;
126: <INITIAL>"}"            return RC;
127: <INITIAL>"["            return LB;
128: <INITIAL>"]"            return RB;
129: <INITIAL>";"            return SEMICOLON;
130: <INITIAL>":"            return COLON;
131: <INITIAL>","            return COMMA;
132: <INITIAL>"&&"           return LOGICAL_AND;
133: <INITIAL>"||"           return LOGICAL_OR;
134: <INITIAL>"="            return ASSIGN_T;
135: <INITIAL>"=="           return EQ;
136: <INITIAL>"!="           return NE;
137: <INITIAL>">"            return GT;
138: <INITIAL>">="           return GE;
139: <INITIAL>"<"            return LT;
140: <INITIAL>"<="           return LE;
141: <INITIAL>"+"            return ADD;
142: <INITIAL>"-"            return SUB;
143: <INITIAL>"*"            return MUL;
144: <INITIAL>"/"            return DIV;
145: <INITIAL>"%"            return MOD;
146: <INITIAL>"+="           return ADD_ASSIGN_T;
147: <INITIAL>"-="           return SUB_ASSIGN_T;
148: <INITIAL>"*="           return MUL_ASSIGN_T;
149: <INITIAL>"/="           return DIV_ASSIGN_T;
150: <INITIAL>"%="           return MOD_ASSIGN_T;
151: <INITIAL>"++"           return INCREMENT;
152: <INITIAL>"--"           return DECREMENT;
153: <INITIAL>"!"            return EXCLAMATION;
154: <INITIAL>"."            return DOT;
155: <INITIAL>[A-Za-z_][A-Za-z_0-9]* {
156:     yylval.identifier = crb_create_identifier(yytext);
157:     return IDENTIFIER;
158: }
159: <INITIAL>[1-9][0-9]* {
160:     Expression  *expression = crb_alloc_expression(INT_EXPRESSION);
161:     sscanf(yytext, "%d", &expression->u.int_value);
162:     yylval.expression = expression;
163:     return INT_LITERAL;
164: }
165: <INITIAL>"0" {
166:     Expression  *expression = crb_alloc_expression(INT_EXPRESSION);
167:     expression->u.int_value = 0;
168:     yylval.expression = expression;
169:     return INT_LITERAL;
170: }
171: <INITIAL>[0-9]+\.[0-9]+ {
172:     Expression  *expression = crb_alloc_expression(DOUBLE_EXPRESSION);
173:     sscanf(yytext, "%lf", &expression->u.double_value);
174:     yylval.expression = expression;
175:     return DOUBLE_LITERAL;
176: }
177: <INITIAL>\" {
178:     crb_open_string_literal();
179:     BEGIN STRING_LITERAL_STATE;
180: }
181: <INITIAL>%%r. {
182:     crb_set_regexp_start_char(yytext[3]);
183:     crb_open_string_literal();
184:     BEGIN REGEXP_LITERAL_STATE;
185: }
186: <INITIAL>[ \t] ;
187: <INITIAL>[ \t\r\n] {increment_line_number();}
188: <INITIAL>#     BEGIN COMMENT;
189: <INITIAL>.      {
190:     crb_compile_error(CHARACTER_INVALID_ERR,
191:                       CRB_CHARACTER_MESSAGE_ARGUMENT, "bad_char", yytext[0],
192:                       CRB_MESSAGE_ARGUMENT_END);
193: }
194: <COMMENT>\n     {
195:     increment_line_number();
196:     BEGIN INITIAL;
197: }
198: <COMMENT>.      ;
199: <STRING_LITERAL_STATE>\"        {
200:     Expression *expression = crb_alloc_expression(STRING_EXPRESSION);
201:     expression->u.string_value = crb_close_string_literal();
202:     yylval.expression = expression;
203:     BEGIN INITIAL;
204:     return STRING_LITERAL;
205: }
206: <STRING_LITERAL_STATE>\n        {
207:     crb_add_string_literal('\n');
208:     increment_line_number();
209: }
210: <STRING_LITERAL_STATE>\\\"      crb_add_string_literal('"');
211: <STRING_LITERAL_STATE>\\n       crb_add_string_literal('\n');
212: <STRING_LITERAL_STATE>\\t       crb_add_string_literal('\t');
213: <STRING_LITERAL_STATE>\\\\      crb_add_string_literal('\\');
214: <STRING_LITERAL_STATE>[\x81-\x9f\xe0-\xef][\x40-\x7e\x80-\xfc] {
215:   crb_add_string_literal(yytext[0]);
216:   crb_add_string_literal(yytext[1]);
217: }
218: <STRING_LITERAL_STATE>.         crb_add_string_literal(yytext[0]);
219: <REGEXP_LITERAL_STATE>\n {
220:     crb_compile_error(CR_IN_REGEXP_ERR, CRB_MESSAGE_ARGUMENT_END);
221: }
222: <REGEXP_LITERAL_STATE>. {
223:     if (yytext[0] != crb_regexp_start_char()) {
224:         crb_add_string_literal(yytext[0]);
225:     } else {
226:         Expression *expression = crb_alloc_expression(REGEXP_EXPRESSION);
227:         CRB_Char *string_literal = crb_close_string_literal();
228:         expression->u.regexp_value
229:             = crb_create_regexp_in_compile(string_literal);
230:         yylval.expression = expression;
231:         BEGIN INITIAL;
232:         return REGEXP_LITERAL;
233:     }
234: }
235: %%
戻る