vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | %{ |
| 2 | |
| 3 | #include <string.h> |
| 4 | #include <errno.h> |
| 5 | #include <assert.h> |
| 6 | |
| 7 | #include "asn1parser.h" |
| 8 | #include "asn1p_y.h" |
| 9 | |
| 10 | int asn1p_lex(void); |
| 11 | void asn1p_lexer_hack_push_opaque_state(void); /* Used in .y */ |
| 12 | void asn1p_lexer_hack_enable_with_syntax(void); /* Used in .y */ |
vlm | 9283dbe | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 13 | void asn1p_lexer_hack_push_encoding_control(void); /* Used in .y */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | |
| 15 | #define YY_FATAL_ERROR(msg) do { \ |
| 16 | fprintf(stderr, \ |
| 17 | "lexer error at line %d, " \ |
| 18 | "text \"%s\"\n", \ |
| 19 | yylineno, yytext); \ |
| 20 | exit(1); \ |
| 21 | } while(0) |
| 22 | |
| 23 | int asn1p_lexer_pedantic_1990 = 0; |
| 24 | int asn1p_lexer_types_year = 0; |
| 25 | int asn1p_lexer_constructs_year = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 26 | |
vlm | 066dc10 | 2005-08-22 12:23:54 +0000 | [diff] [blame] | 27 | int asn1p_as_pointer; |
| 28 | |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 29 | static asn1c_integer_t _lex_atoi(const char *ptr); |
| 30 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 31 | /* |
| 32 | * Check that the type is defined in the year of the standard choosen. |
| 33 | */ |
| 34 | #define TYPE_LIFETIME(fyr, lyr) \ |
| 35 | (!asn1p_lexer_types_year \ |
| 36 | || (fyr && fyr <= asn1p_lexer_types_year) \ |
| 37 | || (lyr && lyr > asn1p_lexer_types_year)) |
| 38 | |
| 39 | /* |
| 40 | * Check the the construction (or concept, i.e. CLASS) is defined in |
| 41 | * a given year. |
| 42 | */ |
| 43 | #define CONSTRUCT_LIFETIME(fyr, lyr) \ |
| 44 | (!asn1p_lexer_constructs_year \ |
| 45 | || (fyr && fyr <= asn1p_lexer_constructs_year) \ |
| 46 | || (lyr && lyr > asn1p_lexer_constructs_year)) |
| 47 | |
| 48 | /* |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | * Append quoted string. |
| 50 | */ |
| 51 | #define QAPPEND(text, tlen) do { \ |
| 52 | char *prev_text = asn1p_lval.tv_opaque.buf; \ |
| 53 | int prev_len = asn1p_lval.tv_opaque.len; \ |
| 54 | char *p; \ |
| 55 | \ |
| 56 | p = malloc((tlen) + prev_len + 1); \ |
| 57 | if(p == NULL) return -1; \ |
| 58 | \ |
| 59 | if(prev_text) memcpy(p, prev_text, prev_len); \ |
| 60 | memcpy(p + prev_len, text, tlen); \ |
| 61 | p[prev_len + (tlen)] = '\0'; \ |
| 62 | \ |
| 63 | free(asn1p_lval.tv_opaque.buf); \ |
| 64 | asn1p_lval.tv_opaque.buf = p; \ |
| 65 | asn1p_lval.tv_opaque.len = (tlen) + prev_len; \ |
| 66 | } while(0) |
| 67 | |
| 68 | %} |
| 69 | |
| 70 | %option never-interactive |
vlm | 9283dbe | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 71 | %option noinput |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 72 | %option noyywrap stack |
| 73 | /* Performance penalty is OK */ |
| 74 | %option yylineno |
| 75 | /* Controlled from within application */ |
| 76 | %option debug |
| 77 | |
| 78 | %pointer |
| 79 | |
| 80 | %x dash_comment |
vlm | b5abdc9 | 2005-07-02 21:42:40 +0000 | [diff] [blame] | 81 | %x idash_comment |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | %x cpp_comment |
| 83 | %x quoted |
| 84 | %x opaque |
vlm | 9283dbe | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 85 | %x encoding_control |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 86 | %x with_syntax |
| 87 | |
| 88 | /* Newline */ |
| 89 | NL [\r\v\f\n] |
| 90 | /* White-space */ |
| 91 | WSP [\t\r\v\f\n ] |
| 92 | |
| 93 | %% |
| 94 | |
vlm | b5abdc9 | 2005-07-02 21:42:40 +0000 | [diff] [blame] | 95 | -{3,}/[\r\n] /* Immediately terminated long comment */ |
| 96 | -{3,}/[^-\r\n] yy_push_state(idash_comment); /* Incorrect, but acceptable */ |
| 97 | <idash_comment>{ |
| 98 | -{3,} yy_pop_state(); /* Acceptable end of comment */ |
| 99 | } |
| 100 | |
vlm | 177a5b6 | 2005-09-05 05:17:57 +0000 | [diff] [blame] | 101 | --<[ \t]*ASN1C.RepresentAsPointer[ \t]*>-- asn1p_as_pointer = 1; |
| 102 | |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 103 | <INITIAL,with_syntax>-- yy_push_state(dash_comment); |
vlm | b5abdc9 | 2005-07-02 21:42:40 +0000 | [diff] [blame] | 104 | <dash_comment,idash_comment>{ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 105 | |
| 106 | {NL} yy_pop_state(); |
| 107 | |
| 108 | -- yy_pop_state(); /* End of comment */ |
| 109 | - /* Eat single dash */ |
vlm | 177a5b6 | 2005-09-05 05:17:57 +0000 | [diff] [blame] | 110 | [^\r\v\f\n-]+ /* Eat */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | } |
vlm | b5abdc9 | 2005-07-02 21:42:40 +0000 | [diff] [blame] | 112 | |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 113 | <INITIAL,cpp_comment,with_syntax>"/*" yy_push_state(cpp_comment); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 114 | <cpp_comment>{ |
vlm | 066dc10 | 2005-08-22 12:23:54 +0000 | [diff] [blame] | 115 | [^*/<] /* Eat */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 116 | "*/" yy_pop_state(); |
| 117 | . /* Eat */ |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /* |
| 122 | * This is state is being set from corresponding .y module when |
| 123 | * higher-level data is necessary to make proper parsing of the |
| 124 | * underlying data. Thus, we enter the <opaque> state and save |
| 125 | * everything for later processing. |
| 126 | */ |
| 127 | <opaque>{ |
| 128 | |
| 129 | "{" { |
| 130 | yy_push_state(opaque); |
| 131 | asn1p_lval.tv_opaque.buf = strdup(yytext); |
| 132 | asn1p_lval.tv_opaque.len = yyleng; |
| 133 | return TOK_opaque; |
| 134 | } |
| 135 | |
| 136 | "}" { |
| 137 | yy_pop_state(); |
| 138 | asn1p_lval.tv_opaque.buf = strdup(yytext); |
| 139 | asn1p_lval.tv_opaque.len = yyleng; |
| 140 | return TOK_opaque; |
| 141 | } |
| 142 | |
| 143 | [^{}:=]+ { |
| 144 | asn1p_lval.tv_opaque.buf = strdup(yytext); |
| 145 | asn1p_lval.tv_opaque.len = yyleng; |
| 146 | return TOK_opaque; |
| 147 | } |
| 148 | |
| 149 | "::=" { |
| 150 | fprintf(stderr, |
| 151 | "ASN.1 Parser syncronization failure: " |
| 152 | "\"%s\" at line %d must not appear " |
| 153 | "inside value definition\n", |
| 154 | yytext, yylineno); |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | [:=] { |
| 159 | asn1p_lval.tv_opaque.buf = strdup(yytext); |
| 160 | asn1p_lval.tv_opaque.len = yyleng; |
| 161 | return TOK_opaque; |
| 162 | } |
| 163 | |
| 164 | } |
| 165 | |
| 166 | \"[^\"]* { |
| 167 | asn1p_lval.tv_opaque.buf = 0; |
| 168 | asn1p_lval.tv_opaque.len = 0; |
| 169 | QAPPEND(yytext+1, yyleng-1); |
| 170 | yy_push_state(quoted); |
| 171 | } |
| 172 | <quoted>{ |
| 173 | |
| 174 | \"\" { QAPPEND(yytext, yyleng-1); } /* Add a single quote */ |
| 175 | [^\"]+ { QAPPEND(yytext, yyleng); } |
| 176 | |
| 177 | \" { |
| 178 | yy_pop_state(); |
| 179 | /* Do not append last quote: |
| 180 | // QAPPEND(yytext, yyleng); */ |
| 181 | |
| 182 | if(asn1p_lexer_pedantic_1990 |
| 183 | && strchr(yytext, '\n')) { |
| 184 | fprintf(stderr, "%s: " |
| 185 | "Newlines are prohibited by ASN.1:1990\n", |
| 186 | asn1p_lval.tv_opaque.buf); |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | return TOK_cstring; |
| 191 | } |
| 192 | |
| 193 | } |
| 194 | |
vlm | 9283dbe | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 195 | <encoding_control>{ |
| 196 | ENCODING-CONTROL { |
| 197 | const char *s = "ENCODING-CONTROL"; |
| 198 | const char *p = s + sizeof("ENCODING-CONTROL") - 2; |
| 199 | for(; p >= s; p--) unput(*p); |
| 200 | yy_pop_state(); |
| 201 | } |
| 202 | END unput('D'); unput('N'); unput('E'); yy_pop_state(); |
| 203 | [^{} \t\r\v\f\n]+ |
| 204 | [[:alnum:]]+ |
| 205 | . /* Eat everything else */ |
| 206 | "\n" |
| 207 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 208 | |
| 209 | '[0-9A-F \t\r\v\f\n]+'H { |
| 210 | /* " \t\r\n" weren't allowed in ASN.1:1990. */ |
| 211 | asn1p_lval.tv_str = yytext; |
| 212 | return TOK_hstring; |
| 213 | } |
| 214 | |
| 215 | '[01 \t\r\v\f\n]+'B { |
| 216 | /* " \t\r\n" weren't allowed in ASN.1:1990. */ |
| 217 | asn1p_lval.tv_str = strdup(yytext); |
| 218 | return TOK_bstring; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | -[1-9][0-9]* { |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 223 | asn1p_lval.a_int = _lex_atoi(yytext); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 224 | if(errno == ERANGE) |
| 225 | return -1; |
| 226 | return TOK_number_negative; |
| 227 | } |
| 228 | |
| 229 | [1-9][0-9]* { |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 230 | asn1p_lval.a_int = _lex_atoi(yytext); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 231 | if(errno == ERANGE) |
| 232 | return -1; |
| 233 | return TOK_number; |
| 234 | } |
| 235 | |
| 236 | "0" { |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 237 | asn1p_lval.a_int = _lex_atoi(yytext); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 238 | if(errno == ERANGE) |
| 239 | return -1; |
| 240 | return TOK_number; |
| 241 | } |
| 242 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 243 | ABSENT return TOK_ABSENT; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 244 | ALL return TOK_ALL; |
| 245 | ANY { |
| 246 | /* Appeared in 1990, removed in 1997 */ |
| 247 | if(TYPE_LIFETIME(1990, 1997)) |
| 248 | return TOK_ANY; |
| 249 | fprintf(stderr, "Keyword \"%s\" at line %d " |
| 250 | "is obsolete\n", yytext, yylineno); |
| 251 | REJECT; |
| 252 | } |
| 253 | APPLICATION return TOK_APPLICATION; |
| 254 | AUTOMATIC return TOK_AUTOMATIC; |
| 255 | BEGIN return TOK_BEGIN; |
| 256 | BIT return TOK_BIT; |
| 257 | BMPString { |
| 258 | if(TYPE_LIFETIME(1994, 0)) |
| 259 | return TOK_BMPString; |
| 260 | REJECT; |
| 261 | } |
| 262 | BOOLEAN return TOK_BOOLEAN; |
| 263 | BY return TOK_BY; |
| 264 | CHARACTER return TOK_CHARACTER; |
| 265 | CHOICE return TOK_CHOICE; |
| 266 | CLASS return TOK_CLASS; |
| 267 | COMPONENT return TOK_COMPONENT; |
| 268 | COMPONENTS return TOK_COMPONENTS; |
vlm | 6611add | 2005-03-20 14:28:32 +0000 | [diff] [blame] | 269 | CONSTRAINED return TOK_CONSTRAINED; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 270 | CONTAINING return TOK_CONTAINING; |
| 271 | DEFAULT return TOK_DEFAULT; |
| 272 | DEFINED { |
| 273 | /* Appeared in 1990, removed in 1997 */ |
| 274 | if(TYPE_LIFETIME(1990, 1997)) |
| 275 | return TOK_DEFINED; |
| 276 | fprintf(stderr, "Keyword \"%s\" at line %d " |
| 277 | "is obsolete\n", yytext, yylineno); |
| 278 | /* Deprecated since */ |
| 279 | REJECT; |
| 280 | } |
| 281 | DEFINITIONS return TOK_DEFINITIONS; |
| 282 | EMBEDDED return TOK_EMBEDDED; |
| 283 | ENCODED return TOK_ENCODED; |
vlm | 9283dbe | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 284 | ENCODING-CONTROL return TOK_ENCODING_CONTROL; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 285 | END return TOK_END; |
| 286 | ENUMERATED return TOK_ENUMERATED; |
| 287 | EXCEPT return TOK_EXCEPT; |
| 288 | EXPLICIT return TOK_EXPLICIT; |
| 289 | EXPORTS return TOK_EXPORTS; |
| 290 | EXTENSIBILITY return TOK_EXTENSIBILITY; |
| 291 | EXTERNAL return TOK_EXTERNAL; |
| 292 | FALSE return TOK_FALSE; |
| 293 | FROM return TOK_FROM; |
| 294 | GeneralizedTime return TOK_GeneralizedTime; |
| 295 | GeneralString return TOK_GeneralString; |
| 296 | GraphicString return TOK_GraphicString; |
| 297 | IA5String return TOK_IA5String; |
| 298 | IDENTIFIER return TOK_IDENTIFIER; |
| 299 | IMPLICIT return TOK_IMPLICIT; |
| 300 | IMPLIED return TOK_IMPLIED; |
| 301 | IMPORTS return TOK_IMPORTS; |
| 302 | INCLUDES return TOK_INCLUDES; |
| 303 | INSTANCE return TOK_INSTANCE; |
vlm | 9283dbe | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 304 | INSTRUCTIONS return TOK_INSTRUCTIONS; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 305 | INTEGER return TOK_INTEGER; |
| 306 | INTERSECTION return TOK_INTERSECTION; |
| 307 | ISO646String return TOK_ISO646String; |
| 308 | MAX return TOK_MAX; |
| 309 | MIN return TOK_MIN; |
| 310 | MINUS-INFINITY return TOK_MINUS_INFINITY; |
| 311 | NULL return TOK_NULL; |
| 312 | NumericString return TOK_NumericString; |
| 313 | OBJECT return TOK_OBJECT; |
| 314 | ObjectDescriptor return TOK_ObjectDescriptor; |
| 315 | OCTET return TOK_OCTET; |
| 316 | OF return TOK_OF; |
| 317 | OPTIONAL return TOK_OPTIONAL; |
| 318 | PATTERN return TOK_PATTERN; |
| 319 | PDV return TOK_PDV; |
| 320 | PLUS-INFINITY return TOK_PLUS_INFINITY; |
| 321 | PRESENT return TOK_PRESENT; |
| 322 | PrintableString return TOK_PrintableString; |
| 323 | PRIVATE return TOK_PRIVATE; |
| 324 | REAL return TOK_REAL; |
| 325 | RELATIVE-OID return TOK_RELATIVE_OID; |
| 326 | SEQUENCE return TOK_SEQUENCE; |
| 327 | SET return TOK_SET; |
| 328 | SIZE return TOK_SIZE; |
| 329 | STRING return TOK_STRING; |
| 330 | SYNTAX return TOK_SYNTAX; |
| 331 | T61String return TOK_T61String; |
| 332 | TAGS return TOK_TAGS; |
| 333 | TeletexString return TOK_TeletexString; |
| 334 | TRUE return TOK_TRUE; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 335 | UNION return TOK_UNION; |
| 336 | UNIQUE return TOK_UNIQUE; |
| 337 | UNIVERSAL return TOK_UNIVERSAL; |
| 338 | UniversalString { |
| 339 | if(TYPE_LIFETIME(1994, 0)) |
| 340 | return TOK_UniversalString; |
| 341 | REJECT; |
| 342 | } |
| 343 | UTCTime return TOK_UTCTime; |
| 344 | UTF8String { |
| 345 | if(TYPE_LIFETIME(1994, 0)) |
| 346 | return TOK_UTF8String; |
| 347 | REJECT; |
| 348 | } |
| 349 | VideotexString return TOK_VideotexString; |
| 350 | VisibleString return TOK_VisibleString; |
| 351 | WITH return TOK_WITH; |
| 352 | |
| 353 | |
vlm | b5abdc9 | 2005-07-02 21:42:40 +0000 | [diff] [blame] | 354 | <INITIAL,with_syntax>&[A-Z][A-Za-z0-9]*([-][A-Za-z0-9]+)* { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 355 | asn1p_lval.tv_str = strdup(yytext); |
| 356 | return TOK_typefieldreference; |
| 357 | } |
| 358 | |
vlm | b5abdc9 | 2005-07-02 21:42:40 +0000 | [diff] [blame] | 359 | <INITIAL,with_syntax>&[a-z][a-zA-Z0-9]*([-][a-zA-Z0-9]+)* { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 360 | asn1p_lval.tv_str = strdup(yytext); |
| 361 | return TOK_valuefieldreference; |
| 362 | } |
| 363 | |
| 364 | |
vlm | b5abdc9 | 2005-07-02 21:42:40 +0000 | [diff] [blame] | 365 | [a-z][a-zA-Z0-9]*([-][a-zA-Z0-9]+)* { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 366 | asn1p_lval.tv_str = strdup(yytext); |
| 367 | return TOK_identifier; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * objectclassreference |
| 372 | */ |
vlm | b5abdc9 | 2005-07-02 21:42:40 +0000 | [diff] [blame] | 373 | [A-Z][A-Z0-9]*([-][A-Z0-9]+)* { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 374 | asn1p_lval.tv_str = strdup(yytext); |
vlm | 9283dbe | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 375 | return TOK_capitalreference; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* |
| 379 | * typereference, modulereference |
| 380 | * NOTE: TOK_objectclassreference must be combined |
| 381 | * with this token to produce true typereference. |
| 382 | */ |
vlm | b5abdc9 | 2005-07-02 21:42:40 +0000 | [diff] [blame] | 383 | [A-Z][A-Za-z0-9]*([-][A-Za-z0-9]+)* { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 384 | asn1p_lval.tv_str = strdup(yytext); |
| 385 | return TOK_typereference; |
| 386 | } |
| 387 | |
| 388 | "::=" return TOK_PPEQ; |
| 389 | |
| 390 | "..." return TOK_ThreeDots; |
| 391 | ".." return TOK_TwoDots; |
| 392 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 393 | <with_syntax>{ |
| 394 | |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 395 | [A-Z][A-Za-z0-9]*([-][A-Za-z0-9]+)* { |
| 396 | asn1p_lval.tv_str = strdup(yytext); |
| 397 | return TOK_Literal; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 398 | } |
| 399 | |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 400 | "," { |
| 401 | asn1p_lval.tv_str = strdup(yytext); |
| 402 | return TOK_Literal; |
| 403 | } |
| 404 | |
| 405 | "{" { |
| 406 | yy_push_state(with_syntax); |
| 407 | asn1p_lval.tv_str = strdup(yytext); |
| 408 | return TOK_Literal; |
| 409 | } |
| 410 | |
| 411 | "[" return '['; |
| 412 | "]" return ']'; |
| 413 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 414 | {WSP}+ { |
| 415 | asn1p_lval.tv_opaque.buf = strdup(yytext); |
| 416 | asn1p_lval.tv_opaque.len = yyleng; |
vlm | eeb3c51 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 417 | return TOK_whitespace; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | "}" { |
| 421 | yy_pop_state(); |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 422 | if(YYSTATE == with_syntax) { |
| 423 | asn1p_lval.tv_str = strdup(yytext); |
| 424 | return TOK_Literal; |
| 425 | } else { |
| 426 | return '}'; |
| 427 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | } |
| 431 | |
vlm | 2c8c44d | 2005-03-24 16:22:35 +0000 | [diff] [blame] | 432 | |
| 433 | {WSP}+ /* Ignore whitespace */ |
| 434 | |
| 435 | |
| 436 | [{][\t\r\v\f\n ]*[0-7][,][\t\r\v\f\n ]*[0-9]+[\t\r\v\f\n ]*[}] { |
| 437 | asn1c_integer_t v1 = -1, v2 = -1; |
| 438 | char *p; |
| 439 | for(p = yytext; *p; p++) |
| 440 | if(*p >= '0' && *p <= '9') |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 441 | { v1 = _lex_atoi(p); break; } |
vlm | 2c8c44d | 2005-03-24 16:22:35 +0000 | [diff] [blame] | 442 | while(*p >= '0' && *p <= '9') p++; /* Skip digits */ |
| 443 | for(; *p; p++) if(*p >= '0' && *p <= '9') |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 444 | { v2 = _lex_atoi(p); break; } |
vlm | 2c8c44d | 2005-03-24 16:22:35 +0000 | [diff] [blame] | 445 | if(v1 < 0 || v1 > 7) { |
| 446 | fprintf(stderr, "%s at line %d: X.680:2003, #37.14 " |
| 447 | "mandates 0..7 range for Tuple's TableColumn\n", |
| 448 | yytext, yylineno); |
| 449 | return -1; |
| 450 | } |
| 451 | if(v2 < 0 || v2 > 15) { |
| 452 | fprintf(stderr, "%s at line %d: X.680:2003, #37.14 " |
| 453 | "mandates 0..15 range for Tuple's TableRow\n", |
| 454 | yytext, yylineno); |
| 455 | return -1; |
| 456 | } |
| 457 | asn1p_lval.a_int = (v1 << 4) + v2; |
| 458 | return TOK_tuple; |
| 459 | } |
| 460 | |
| 461 | [{][\t\r\v\f\n ]*[0-9]+[,][\t\r\v\f\n ]*[0-9]+[,][\t\r\v\f\n ]*[0-9]+[,][\t\r\v\f\n ]*[0-9]+[\t\r\v\f\n ]*[}] { |
| 462 | asn1c_integer_t v1 = -1, v2 = -1, v3 = -1, v4 = -1; |
| 463 | char *p; |
| 464 | for(p = yytext; *p; p++) |
| 465 | if(*p >= '0' && *p <= '9') |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 466 | { v1 = _lex_atoi(p); break; } |
vlm | 2c8c44d | 2005-03-24 16:22:35 +0000 | [diff] [blame] | 467 | while(*p >= '0' && *p <= '9') p++; /* Skip digits */ |
| 468 | for(; *p; p++) if(*p >= '0' && *p <= '9') |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 469 | { v2 = _lex_atoi(p); break; } |
vlm | 2c8c44d | 2005-03-24 16:22:35 +0000 | [diff] [blame] | 470 | while(*p >= '0' && *p <= '9') p++; |
| 471 | for(; *p; p++) if(*p >= '0' && *p <= '9') |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 472 | { v3 = _lex_atoi(p); break; } |
vlm | 2c8c44d | 2005-03-24 16:22:35 +0000 | [diff] [blame] | 473 | while(*p >= '0' && *p <= '9') p++; |
| 474 | for(; *p; p++) if(*p >= '0' && *p <= '9') |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 475 | { v4 = _lex_atoi(p); break; } |
vlm | 2c8c44d | 2005-03-24 16:22:35 +0000 | [diff] [blame] | 476 | if(v1 < 0 || v1 > 127) { |
| 477 | fprintf(stderr, "%s at line %d: X.680:2003, #37.12 " |
| 478 | "mandates 0..127 range for Quadruple's Group\n", |
| 479 | yytext, yylineno); |
| 480 | return -1; |
| 481 | } |
| 482 | if(v2 < 0 || v2 > 255) { |
| 483 | fprintf(stderr, "%s at line %d: X.680:2003, #37.12 " |
| 484 | "mandates 0..255 range for Quadruple's Plane\n", |
| 485 | yytext, yylineno); |
| 486 | return -1; |
| 487 | } |
| 488 | if(v3 < 0 || v3 > 255) { |
| 489 | fprintf(stderr, "%s at line %d: X.680:2003, #37.12 " |
| 490 | "mandates 0..255 range for Quadruple's Row\n", |
| 491 | yytext, yylineno); |
| 492 | return -1; |
| 493 | } |
| 494 | if(v4 < 0 || v4 > 255) { |
| 495 | fprintf(stderr, "%s at line %d: X.680:2003, #37.12 " |
| 496 | "mandates 0..255 range for Quadruple's Cell\n", |
| 497 | yytext, yylineno); |
| 498 | return -1; |
| 499 | } |
| 500 | asn1p_lval.a_int = (v1 << 24) | (v2 << 16) | (v3 << 8) | v4; |
| 501 | return TOK_quadruple; |
| 502 | } |
| 503 | |
| 504 | |
| 505 | [(){},;:|!.&@\[\]^] return yytext[0]; |
| 506 | |
| 507 | [^A-Za-z0-9:=,{}<.@()[]'\"|&^*;!-] { |
| 508 | if(TYPE_LIFETIME(1994, 0)) |
| 509 | fprintf(stderr, "ERROR: "); |
| 510 | fprintf(stderr, |
| 511 | "Symbol '%c' at line %d is prohibited " |
| 512 | "by ASN.1:1994 and ASN.1:1997\n", |
| 513 | yytext[0], yylineno); |
| 514 | if(TYPE_LIFETIME(1994, 0)) |
| 515 | return -1; |
| 516 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 517 | |
| 518 | <*>. { |
| 519 | fprintf(stderr, |
| 520 | "Unexpected token at line %d: \"%s\"\n", |
| 521 | yylineno, yytext); |
| 522 | while(YYSTATE != INITIAL) |
| 523 | yy_pop_state(); |
vlm | c94e28f | 2004-09-15 11:59:51 +0000 | [diff] [blame] | 524 | if(0) { |
| 525 | yy_top_state(); /* Just to use this function. */ |
| 526 | yy_fatal_error("Parse error"); |
| 527 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 528 | return -1; |
| 529 | } |
| 530 | |
| 531 | <*><<EOF>> { |
| 532 | while(YYSTATE != INITIAL) |
| 533 | yy_pop_state(); |
| 534 | yyterminate(); |
| 535 | } |
| 536 | |
| 537 | |
| 538 | %% |
| 539 | |
| 540 | /* |
| 541 | * Very dirty but wonderful hack allowing to rule states from within .y file. |
| 542 | */ |
vlm | 9283dbe | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 543 | void asn1p_lexer_hack_push_opaque_state() { yy_push_state(opaque); } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 544 | |
| 545 | /* |
| 546 | * Another hack which disables recognizing some tokens when inside WITH SYNTAX. |
| 547 | */ |
vlm | 9283dbe | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 548 | void asn1p_lexer_hack_enable_with_syntax() { yy_push_state(with_syntax); } |
| 549 | |
| 550 | /* Yet another */ |
| 551 | void asn1p_lexer_hack_push_encoding_control() { |
| 552 | yy_push_state(encoding_control); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 553 | } |
| 554 | |
vlm | 2728a8d | 2005-01-23 09:51:44 +0000 | [diff] [blame] | 555 | static asn1c_integer_t |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 556 | _lex_atoi(const char *ptr) { |
vlm | 2728a8d | 2005-01-23 09:51:44 +0000 | [diff] [blame] | 557 | asn1c_integer_t value; |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 558 | if(asn1p_atoi(ptr, &value)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 559 | fprintf(stderr, |
| 560 | "Value \"%s\" at line %d is too large " |
vlm | 86912f0 | 2005-04-05 08:46:22 +0000 | [diff] [blame] | 561 | "for this compiler! Please contact the asn1c author.\n", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 562 | ptr, yylineno); |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 563 | errno = ERANGE; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 564 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 565 | return value; |
| 566 | } |