| 
									
										
										
										
											2012-08-16 01:00:30 +01:00
										 |  |  | %{ | 
					
						
							| 
									
										
										
										
											2012-12-18 16:52:47 +00:00
										 |  |  | #include "jv_alloc.h" | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:30 +01:00
										 |  |  | #include "compile.h" | 
					
						
							| 
									
										
										
										
											2012-10-24 09:28:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct lexer_param; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-18 17:44:43 +01:00
										 |  |  | #include "parser.gen.h"  /* Generated by bison. */ | 
					
						
							| 
									
										
										
										
											2012-09-11 00:04:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define YY_USER_ACTION                           \ | 
					
						
							|  |  |  |   do {                                           \ | 
					
						
							|  |  |  |     yylloc->start = yyget_extra(yyscanner);      \ | 
					
						
							|  |  |  |     yylloc->end = yylloc->start + yyleng;        \ | 
					
						
							|  |  |  |     yyset_extra(yylloc->end, yyscanner);         \ | 
					
						
							|  |  |  |   } while (0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:30 +01:00
										 |  |  | %} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-17 20:41:57 +01:00
										 |  |  | %s IN_PAREN | 
					
						
							|  |  |  | %s IN_BRACKET | 
					
						
							|  |  |  | %s IN_BRACE | 
					
						
							|  |  |  | %s IN_QQINTERP | 
					
						
							|  |  |  | %x IN_QQSTRING | 
					
						
							|  |  |  | %{ | 
					
						
							|  |  |  |   static int enter(int opening, int state, yyscan_t yyscanner); | 
					
						
							|  |  |  |   static int try_exit(int closing, int state, yyscan_t yyscanner); | 
					
						
							|  |  |  | %} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:30 +01:00
										 |  |  | %option noyywrap nounput noinput nodefault | 
					
						
							| 
									
										
										
										
											2012-12-18 16:52:47 +00:00
										 |  |  | %option noyyalloc noyyrealloc noyyfree | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:30 +01:00
										 |  |  | %option reentrant | 
					
						
							| 
									
										
										
										
											2012-09-11 00:04:47 +01:00
										 |  |  | %option extra-type="int" | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:30 +01:00
										 |  |  | %option bison-bridge bison-locations | 
					
						
							| 
									
										
										
										
											2012-09-11 09:13:20 +01:00
										 |  |  | %option prefix="jq_yy" | 
					
						
							| 
									
										
										
										
											2012-09-17 20:41:57 +01:00
										 |  |  | %option stack | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | %% | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-22 19:16:27 +01:00
										 |  |  | "#"[^\r\n]* { /* comments */ } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-23 17:01:39 +02:00
										 |  |  | "!=" { return NEQ; } | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:30 +01:00
										 |  |  | "==" { return EQ; } | 
					
						
							|  |  |  | "as" { return AS; } | 
					
						
							| 
									
										
										
										
											2012-08-21 18:14:13 +01:00
										 |  |  | "def" { return DEF; } | 
					
						
							| 
									
										
										
										
											2012-09-04 15:34:34 +01:00
										 |  |  | "if" { return IF; } | 
					
						
							|  |  |  | "then" { return THEN; } | 
					
						
							|  |  |  | "else" { return ELSE; } | 
					
						
							| 
									
										
										
										
											2012-09-04 16:05:24 +01:00
										 |  |  | "elif" { return ELSE_IF; } | 
					
						
							| 
									
										
										
										
											2012-09-04 20:38:59 +01:00
										 |  |  | "and" { return AND; } | 
					
						
							|  |  |  | "or" { return OR; } | 
					
						
							| 
									
										
										
										
											2012-09-04 15:34:34 +01:00
										 |  |  | "end" { return END; } | 
					
						
							| 
									
										
										
										
											2012-12-28 15:04:16 +00:00
										 |  |  | "fold" { return FOLD; } | 
					
						
							| 
									
										
										
										
											2012-09-04 15:34:34 +01:00
										 |  |  | "//" { return DEFINEDOR; } | 
					
						
							| 
									
										
										
										
											2012-09-11 23:55:59 +01:00
										 |  |  | "|=" { return SETPIPE; } | 
					
						
							|  |  |  | "+=" { return SETPLUS; } | 
					
						
							|  |  |  | "-=" { return SETMINUS; } | 
					
						
							|  |  |  | "*=" { return SETMULT; } | 
					
						
							|  |  |  | "/=" { return SETDIV; } | 
					
						
							|  |  |  | "//=" { return SETDEFINEDOR; } | 
					
						
							| 
									
										
										
										
											2012-10-07 22:34:12 +01:00
										 |  |  | "<=" { return LESSEQ; } | 
					
						
							|  |  |  | ">=" { return GREATEREQ; } | 
					
						
							|  |  |  | "."|"="|";"|","|":"|"|"|"+"|"-"|"*"|"/"|"\$"|"<"|">" { return yytext[0];} | 
					
						
							| 
									
										
										
										
											2012-09-17 20:41:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | "["|"{"|"(" { | 
					
						
							|  |  |  |   return enter(yytext[0], YY_START, yyscanner); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "]"|"}"|")" { | 
					
						
							|  |  |  |   return try_exit(yytext[0], YY_START, yyscanner); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-27 20:49:34 +00:00
										 |  |  | "@"[a-zA-Z0-9_]+ { | 
					
						
							|  |  |  |   yylval->literal = jv_string_sized(yytext + 1, yyleng - 1); return FORMAT; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-03 12:53:06 +00:00
										 |  |  | [0-9.]+([eE][+-]?[0-9]+)? {  | 
					
						
							| 
									
										
										
										
											2012-09-03 13:36:12 +01:00
										 |  |  |    yylval->literal = jv_parse_sized(yytext, yyleng); return LITERAL;  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-20 00:09:20 +01:00
										 |  |  | "\"" { | 
					
						
							| 
									
										
										
										
											2012-09-17 20:41:57 +01:00
										 |  |  |   yy_push_state(IN_QQSTRING, yyscanner); | 
					
						
							|  |  |  |   return QQSTRING_START; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <IN_QQSTRING>{ | 
					
						
							| 
									
										
										
										
											2012-10-20 00:09:20 +01:00
										 |  |  |   "\\(" { | 
					
						
							| 
									
										
										
										
											2012-09-17 20:41:57 +01:00
										 |  |  |     return enter(QQSTRING_INTERP_START, YY_START, yyscanner); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-10-20 00:09:20 +01:00
										 |  |  |   "\"" { | 
					
						
							| 
									
										
										
										
											2012-09-17 20:41:57 +01:00
										 |  |  |     yy_pop_state(yyscanner); | 
					
						
							|  |  |  |     return QQSTRING_END; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-10-20 00:09:20 +01:00
										 |  |  |   (\\[^u(]|\\u[a-zA-Z0-9]{0,4})+ { | 
					
						
							| 
									
										
										
										
											2012-09-18 13:22:22 +01:00
										 |  |  |     /* pass escapes to the json parser */ | 
					
						
							|  |  |  |     jv escapes = jv_string_fmt("\"%.*s\"", yyleng, yytext); | 
					
						
							|  |  |  |     yylval->literal = jv_parse_sized(jv_string_value(escapes), jv_string_length(jv_copy(escapes))); | 
					
						
							|  |  |  |     jv_free(escapes); | 
					
						
							|  |  |  |     return QQSTRING_TEXT; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-10-20 00:09:20 +01:00
										 |  |  |   [^\\\"]+ { | 
					
						
							| 
									
										
										
										
											2012-09-18 13:22:22 +01:00
										 |  |  |     yylval->literal = jv_string_sized(yytext, yyleng); | 
					
						
							|  |  |  |     return QQSTRING_TEXT; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   . { | 
					
						
							|  |  |  |     return INVALID_CHARACTER; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-09-17 20:41:57 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2012-09-11 10:12:25 +01:00
										 |  |  |    | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-22 14:03:46 +01:00
										 |  |  | [a-zA-Z_][a-zA-Z_0-9]*  { yylval->literal = jv_string(yytext); return IDENT;} | 
					
						
							| 
									
										
										
										
											2012-09-03 13:36:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:30 +01:00
										 |  |  | [ \n\t]+  {} | 
					
						
							| 
									
										
										
										
											2012-09-03 13:36:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-11 09:13:20 +01:00
										 |  |  | . { return INVALID_CHARACTER; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:30 +01:00
										 |  |  | %% | 
					
						
							|  |  |  | /* perhaps these should be calls... */ | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | "true" { return TRUE; } | 
					
						
							|  |  |  | "false" { return FALSE; } | 
					
						
							|  |  |  | "null" { return NULL; } | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2012-09-17 20:41:57 +01:00
										 |  |  | static int try_exit(int c, int state, yyscan_t yyscanner) { | 
					
						
							|  |  |  |   char match = 0; | 
					
						
							|  |  |  |   int ret; | 
					
						
							|  |  |  |   switch (state) { | 
					
						
							|  |  |  |   case IN_PAREN: match = ret = ')'; break; | 
					
						
							|  |  |  |   case IN_BRACKET: match = ret = ']'; break; | 
					
						
							|  |  |  |   case IN_BRACE: match = ret = '}'; break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   case IN_QQINTERP: | 
					
						
							|  |  |  |     match = ')';  | 
					
						
							|  |  |  |     ret = QQSTRING_INTERP_END; | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2012-11-26 01:40:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   default: | 
					
						
							|  |  |  |     // may not be the best error to give | 
					
						
							|  |  |  |     return INVALID_CHARACTER; | 
					
						
							| 
									
										
										
										
											2012-09-17 20:41:57 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   assert(match); | 
					
						
							|  |  |  |   if (match == c) { | 
					
						
							|  |  |  |     yy_pop_state(yyscanner); | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     // FIXME: should we pop? Give a better error at least | 
					
						
							|  |  |  |     return INVALID_CHARACTER; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int enter(int c, int currstate, yyscan_t yyscanner) { | 
					
						
							|  |  |  |   int state = 0; | 
					
						
							|  |  |  |   switch (c) { | 
					
						
							|  |  |  |   case '(': state = IN_PAREN; break; | 
					
						
							|  |  |  |   case '[': state = IN_BRACKET; break; | 
					
						
							|  |  |  |   case '{': state = IN_BRACE; break; | 
					
						
							|  |  |  |   case QQSTRING_INTERP_START: state = IN_QQINTERP; break; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   assert(state); | 
					
						
							|  |  |  |   yy_push_state(state, yyscanner); | 
					
						
							|  |  |  |   return c; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-12-18 16:52:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void* yyalloc(size_t sz, void* extra) { | 
					
						
							|  |  |  |   return jv_mem_alloc(sz); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | void* yyrealloc(void* p, size_t sz, void* extra) { | 
					
						
							|  |  |  |   return jv_mem_realloc(p, sz); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | void yyfree(void* p, void* extra) { | 
					
						
							|  |  |  |   jv_mem_free(p); | 
					
						
							|  |  |  | } |