crowbar.l(Ver.0.4)
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>"return" return RETURN_T;
110: <INITIAL>"break" return BREAK;
111: <INITIAL>"continue" return CONTINUE;
112: <INITIAL>"null" return NULL_T;
113: <INITIAL>"true" return TRUE_T;
114: <INITIAL>"false" return FALSE_T;
115: <INITIAL>"closure" return CLOSURE;
116: <INITIAL>"global" return GLOBAL_T;
117: <INITIAL>"try" return TRY;
118: <INITIAL>"catch" return CATCH;
119: <INITIAL>"finally" return FINALLY;
120: <INITIAL>"throw" return THROW;
121: <INITIAL>"(" return LP;
122: <INITIAL>")" return RP;
123: <INITIAL>"{" return LC;
124: <INITIAL>"}" return RC;
125: <INITIAL>"[" return LB;
126: <INITIAL>"]" return RB;
127: <INITIAL>";" return SEMICOLON;
128: <INITIAL>"," return COMMA;
129: <INITIAL>"&&" return LOGICAL_AND;
130: <INITIAL>"||" return LOGICAL_OR;
131: <INITIAL>"=" return ASSIGN_T;
132: <INITIAL>"==" return EQ;
133: <INITIAL>"!=" return NE;
134: <INITIAL>">" return GT;
135: <INITIAL>">=" return GE;
136: <INITIAL>"<" return LT;
137: <INITIAL>"<=" return LE;
138: <INITIAL>"+" return ADD;
139: <INITIAL>"-" return SUB;
140: <INITIAL>"*" return MUL;
141: <INITIAL>"/" return DIV;
142: <INITIAL>"%" return MOD;
143: <INITIAL>"+=" return ADD_ASSIGN_T;
144: <INITIAL>"-=" return SUB_ASSIGN_T;
145: <INITIAL>"*=" return MUL_ASSIGN_T;
146: <INITIAL>"/=" return DIV_ASSIGN_T;
147: <INITIAL>"%=" return MOD_ASSIGN_T;
148: <INITIAL>"++" return INCREMENT;
149: <INITIAL>"--" return DECREMENT;
150: <INITIAL>"!" return EXCLAMATION;
151: <INITIAL>"." return DOT;
152: <INITIAL>[A-Za-z_][A-Za-z_0-9]* {
153: yylval.identifier = crb_create_identifier(yytext);
154: return IDENTIFIER;
155: }
156: <INITIAL>[1-9][0-9]* {
157: Expression *expression = crb_alloc_expression(INT_EXPRESSION);
158: sscanf(yytext, "%d", &expression->u.int_value);
159: yylval.expression = expression;
160: return INT_LITERAL;
161: }
162: <INITIAL>"0" {
163: Expression *expression = crb_alloc_expression(INT_EXPRESSION);
164: expression->u.int_value = 0;
165: yylval.expression = expression;
166: return INT_LITERAL;
167: }
168: <INITIAL>[0-9]+\.[0-9]+ {
169: Expression *expression = crb_alloc_expression(DOUBLE_EXPRESSION);
170: sscanf(yytext, "%lf", &expression->u.double_value);
171: yylval.expression = expression;
172: return DOUBLE_LITERAL;
173: }
174: <INITIAL>\" {
175: crb_open_string_literal();
176: BEGIN STRING_LITERAL_STATE;
177: }
178: <INITIAL>%%r. {
179: crb_set_regexp_start_char(yytext[3]);
180: crb_open_string_literal();
181: BEGIN REGEXP_LITERAL_STATE;
182: }
183: <INITIAL>[ \t] ;
184: <INITIAL>[ \t\r\n] {increment_line_number();}
185: <INITIAL># BEGIN COMMENT;
186: <INITIAL>. {
187: crb_compile_error(CHARACTER_INVALID_ERR,
188: CRB_CHARACTER_MESSAGE_ARGUMENT, "bad_char", yytext[0],
189: CRB_MESSAGE_ARGUMENT_END);
190: }
191: <COMMENT>\n {
192: increment_line_number();
193: BEGIN INITIAL;
194: }
195: <COMMENT>. ;
196: <STRING_LITERAL_STATE>\" {
197: Expression *expression = crb_alloc_expression(STRING_EXPRESSION);
198: expression->u.string_value = crb_close_string_literal();
199: yylval.expression = expression;
200: BEGIN INITIAL;
201: return STRING_LITERAL;
202: }
203: <STRING_LITERAL_STATE>\n {
204: crb_add_string_literal('\n');
205: increment_line_number();
206: }
207: <STRING_LITERAL_STATE>\\\" crb_add_string_literal('"');
208: <STRING_LITERAL_STATE>\\n crb_add_string_literal('\n');
209: <STRING_LITERAL_STATE>\\t crb_add_string_literal('\t');
210: <STRING_LITERAL_STATE>\\\\ crb_add_string_literal('\\');
211: <STRING_LITERAL_STATE>[\x81-\x9f\xe0-\xef][\x40-\x7e\x80-\xfc] {
212: crb_add_string_literal(yytext[0]);
213: crb_add_string_literal(yytext[1]);
214: }
215: <STRING_LITERAL_STATE>. crb_add_string_literal(yytext[0]);
216: <REGEXP_LITERAL_STATE>\n {
217: crb_compile_error(CR_IN_REGEXP_ERR, CRB_MESSAGE_ARGUMENT_END);
218: }
219: <REGEXP_LITERAL_STATE>. {
220: if (yytext[0] != crb_regexp_start_char()) {
221: crb_add_string_literal(yytext[0]);
222: } else {
223: Expression *expression = crb_alloc_expression(REGEXP_EXPRESSION);
224: CRB_Char *string_literal = crb_close_string_literal();
225: expression->u.regexp_value
226: = crb_create_regexp_in_compile(string_literal);
227: yylval.expression = expression;
228: BEGIN INITIAL;
229: return REGEXP_LITERAL;
230: }
231: }
232: %%
戻る