Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 1 | %{ |
| 2 | /* |
| 3 | * Copyright (C) 2009 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "expr.h" |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 19 | #include "yydefs.h" |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 20 | #include "parser.h" |
| 21 | |
| 22 | int gLine = 1; |
| 23 | int gColumn = 1; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 24 | int gPos = 0; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 25 | |
| 26 | // TODO: enforce MAX_STRING_LEN during lexing |
| 27 | char string_buffer[MAX_STRING_LEN]; |
| 28 | char* string_pos; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 29 | |
| 30 | #define ADVANCE do {yylloc.start=gPos; yylloc.end=gPos+yyleng; \ |
| 31 | gColumn+=yyleng; gPos+=yyleng;} while(0) |
| 32 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 33 | %} |
| 34 | |
| 35 | %x STR |
| 36 | |
| 37 | %option noyywrap |
| 38 | |
| 39 | %% |
| 40 | |
| 41 | |
| 42 | \" { |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 43 | BEGIN(STR); |
| 44 | string_pos = string_buffer; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 45 | yylloc.start = gPos; |
| 46 | ++gColumn; |
| 47 | ++gPos; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | <STR>{ |
| 51 | \" { |
| 52 | ++gColumn; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 53 | ++gPos; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 54 | BEGIN(INITIAL); |
| 55 | *string_pos = '\0'; |
| 56 | yylval.str = strdup(string_buffer); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 57 | yylloc.end = gPos; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 58 | return STRING; |
| 59 | } |
| 60 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 61 | \\n { gColumn += yyleng; gPos += yyleng; *string_pos++ = '\n'; } |
| 62 | \\t { gColumn += yyleng; gPos += yyleng; *string_pos++ = '\t'; } |
| 63 | \\\" { gColumn += yyleng; gPos += yyleng; *string_pos++ = '\"'; } |
| 64 | \\\\ { gColumn += yyleng; gPos += yyleng; *string_pos++ = '\\'; } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 65 | |
| 66 | \\x[0-9a-fA-F]{2} { |
| 67 | gColumn += yyleng; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 68 | gPos += yyleng; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 69 | int val; |
| 70 | sscanf(yytext+2, "%x", &val); |
| 71 | *string_pos++ = val; |
| 72 | } |
| 73 | |
| 74 | \n { |
| 75 | ++gLine; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 76 | ++gPos; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 77 | gColumn = 1; |
| 78 | *string_pos++ = yytext[0]; |
| 79 | } |
| 80 | |
| 81 | . { |
| 82 | ++gColumn; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 83 | ++gPos; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 84 | *string_pos++ = yytext[0]; |
| 85 | } |
| 86 | } |
| 87 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 88 | if ADVANCE; return IF; |
| 89 | then ADVANCE; return THEN; |
| 90 | else ADVANCE; return ELSE; |
| 91 | endif ADVANCE; return ENDIF; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 92 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 93 | [a-zA-Z0-9_:/.]+ { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 94 | ADVANCE; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 95 | yylval.str = strdup(yytext); |
| 96 | return STRING; |
| 97 | } |
| 98 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 99 | \&\& ADVANCE; return AND; |
| 100 | \|\| ADVANCE; return OR; |
| 101 | == ADVANCE; return EQ; |
| 102 | != ADVANCE; return NE; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 103 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 104 | [+(),!;] ADVANCE; return yytext[0]; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 105 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 106 | [ \t]+ ADVANCE; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 107 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 108 | (#.*)?\n gPos += yyleng; ++gLine; gColumn = 1; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 109 | |
| 110 | . return BAD; |