Damien Zammit | 0685322 | 2016-11-16 21:06:54 +1100 | [diff] [blame] | 1 | /* |
| 2 | * blobtool - Compiler/Decompiler for data blobs with specs |
| 3 | * Copyright (C) 2017 Damien Zammit <damien@zamaudio.com> |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | %{ |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include "blobtool.tab.h" |
| 21 | |
| 22 | extern struct blob binary; |
| 23 | |
| 24 | unsigned int parsehex (char *s) |
| 25 | { |
| 26 | unsigned int i, nib, val = 0; |
| 27 | unsigned int nibs = strlen(s) - 2; |
| 28 | |
| 29 | for (i = 2; i < nibs + 2; i++) { |
| 30 | if (s[i] >= '0' && s[i] <= '9') { |
| 31 | nib = s[i] - '0'; |
| 32 | } else if (s[i] >= 'a' && s[i] <= 'f') { |
| 33 | nib = s[i] - 'a' + 10; |
| 34 | } else if (s[i] >= 'A' && s[i] <= 'F') { |
| 35 | nib = s[i] - 'A' + 10; |
| 36 | } else { |
| 37 | return 0; |
| 38 | } |
| 39 | val |= nib << (((nibs - 1) - (i - 2)) * 4); |
| 40 | } |
| 41 | return val; |
| 42 | } |
| 43 | |
| 44 | char* stripquotes (char *string) |
| 45 | { |
| 46 | char *stripped; |
| 47 | unsigned int len = strlen(string); |
| 48 | if (len >= 2 && string[0] == '\"' && string[len-1] == '\"') { |
| 49 | stripped = (char *) malloc (len - 2 + 1); |
| 50 | snprintf (stripped, len - 2 + 1, "%s", string+1); |
| 51 | stripped[len-2] = '\0'; |
| 52 | return stripped; |
| 53 | } else { |
| 54 | return 0; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | %} |
| 59 | |
| 60 | %option noyywrap |
| 61 | %option nounput |
| 62 | |
| 63 | DIGIT1to9 [1-9] |
| 64 | DIGIT [0-9] |
| 65 | DIGITS {DIGIT}+ |
| 66 | INT {DIGIT}|{DIGIT1to9}{DIGITS}|-{DIGIT}|-{DIGIT1to9}{DIGITS} |
| 67 | FRAC [.]{DIGITS} |
| 68 | E [eE][+-]? |
| 69 | EXP {E}{DIGITS} |
| 70 | HEX_DIGIT [0-9a-fA-F] |
| 71 | HEX_DIGITS {HEX_DIGIT}+ |
| 72 | NUMBER {INT}|{INT}{FRAC}|{INT}{EXP}|{INT}{FRAC}{EXP} |
| 73 | UNICODECHAR \\u{HEX_DIGIT}{HEX_DIGIT}{HEX_DIGIT}{HEX_DIGIT} |
| 74 | ALPHA [a-zA-Z] |
| 75 | SPECIAL [()\[\]"'@_\-+:;/\\.,<> ] |
| 76 | VARCHAR {ALPHA}|{DIGIT}|{SPECIAL} |
| 77 | CHAR {VARCHAR}|{UNICODECHAR} |
| 78 | CHARS {CHAR}+ |
| 79 | QUOTE ["] |
| 80 | HEX_PREFIX [0][x] |
| 81 | HEX {HEX_PREFIX}{HEX_DIGITS} |
| 82 | STRING {QUOTE}{QUOTE}|{QUOTE}{CHARS}{QUOTE} |
| 83 | COMMENT [#]{CHARS}[\n]|[#]\n |
| 84 | |
| 85 | %% |
| 86 | |
| 87 | {STRING} { |
| 88 | yylval.str = stripquotes(yytext); |
| 89 | return name; |
| 90 | }; |
| 91 | |
| 92 | {NUMBER} { |
| 93 | yylval.u32 = atoi(yytext); |
| 94 | return val; |
| 95 | }; |
| 96 | |
| 97 | {HEX} { |
| 98 | yylval.u32 = parsehex(yytext); |
| 99 | return val; |
| 100 | }; |
| 101 | |
| 102 | \{ { |
| 103 | return '{'; |
| 104 | }; |
| 105 | |
| 106 | \} { |
| 107 | return '}'; |
| 108 | }; |
| 109 | |
| 110 | \[ { |
| 111 | return '['; |
| 112 | }; |
| 113 | |
| 114 | \] { |
| 115 | return ']'; |
| 116 | }; |
| 117 | |
| 118 | , { |
| 119 | return ','; |
| 120 | }; |
| 121 | |
| 122 | : { |
| 123 | return ':'; |
| 124 | }; |
| 125 | |
| 126 | = { |
| 127 | return '='; |
| 128 | }; |
| 129 | |
| 130 | [ \t\n]+ /* ignore whitespace */; |
| 131 | |
| 132 | {COMMENT} /* ignore comments */ |
| 133 | |
| 134 | \% { |
| 135 | return '%'; |
| 136 | }; |
| 137 | |
| 138 | <<EOF>> { return eof; }; |
| 139 | |
| 140 | %% |
| 141 | |
| 142 | void set_input_string(char* in) { |
| 143 | yy_scan_string(in); |
| 144 | } |
| 145 | |