Root/
| 1 | %{ |
| 2 | /* |
| 3 | * lang.l - BOOM syntax |
| 4 | * |
| 5 | * Copyright 2012 by Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <stdarg.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <unistd.h> |
| 16 | #include <fcntl.h> |
| 17 | |
| 18 | #include "util.h" |
| 19 | #include "param.h" |
| 20 | #include "chr.h" |
| 21 | #include "bom.h" |
| 22 | #include "y.tab.h" |
| 23 | #include "lang.h" |
| 24 | |
| 25 | |
| 26 | extern int yyparse(void); |
| 27 | |
| 28 | const char *file_name_override; |
| 29 | |
| 30 | static int start_token; |
| 31 | static int expose_nl; /* 0: ignore \n; 1: return TOK_NL */ |
| 32 | static int pattern; /* 0: = relops are normal; 1: relops switch to PAT */ |
| 33 | static int hash; /* number of hashes seen in BOM mode */ |
| 34 | static const char *file_name; |
| 35 | static int lineno; |
| 36 | |
| 37 | |
| 38 | static void open_stdin(const char *name) |
| 39 | { |
| 40 | int fd; |
| 41 | |
| 42 | fd = open(name, O_RDONLY); |
| 43 | if (fd < 0) { |
| 44 | perror(name); |
| 45 | exit(1); |
| 46 | } |
| 47 | if (dup2(fd, 0) < 0) { |
| 48 | perror("dup2"); |
| 49 | exit(1); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | static void do_parse(const char *name, int start, int nl, int pat) |
| 55 | { |
| 56 | open_stdin(name); |
| 57 | |
| 58 | start_token = start; |
| 59 | expose_nl = nl; |
| 60 | pattern = pat; |
| 61 | file_name = file_name_override ? file_name_override : unique(name); |
| 62 | file_name_override = NULL; |
| 63 | lineno = 1; |
| 64 | yyparse(); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | void parse_hierarchy(const char *name) |
| 69 | { |
| 70 | do_parse(name, START_HIERARCHY, 0, 0); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void parse_characteristics(const char *name) |
| 75 | { |
| 76 | do_parse(name, START_CHAR, 1, 0); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | void parse_inventory(const char *name) |
| 81 | { |
| 82 | do_parse(name, START_INVENTORY, 1, 0); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | void parse_currencies(const char *name) |
| 87 | { |
| 88 | do_parse(name, START_EXCHANGE, 1, 0); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void parse_providers(const char *name) |
| 93 | { |
| 94 | do_parse(name, START_PROVIDERS, 1, 0); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void parse_substitutions(const char *name) |
| 99 | { |
| 100 | do_parse(name, START_SUBST, 0, 1); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | void parse_symbols(const char *name) |
| 105 | { |
| 106 | do_parse(name, START_SYMBOLS, 1, 0); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static void process_bom_line(const char *s) |
| 111 | { |
| 112 | struct bom *b; |
| 113 | |
| 114 | b = bom_parse_line(s); |
| 115 | bom_subst(b, substitutions); |
| 116 | bom_dump(stderr, b); |
| 117 | } |
| 118 | |
| 119 | %} |
| 120 | |
| 121 | |
| 122 | /* |
| 123 | * We use ID for a bit of a hack: let %... be recognized as '%' WORD but treat |
| 124 | * ...% still as a single WORD. |
| 125 | */ |
| 126 | |
| 127 | ID [-_A-Za-z0-9()+./,] |
| 128 | PAT "\\"[^\t\n]|[^ \t\n\\] |
| 129 | |
| 130 | %s ID PAT |
| 131 | %x BOM |
| 132 | |
| 133 | %% |
| 134 | |
| 135 | %{ |
| 136 | if (start_token) { |
| 137 | int tmp = start_token; |
| 138 | |
| 139 | start_token = 0; |
| 140 | return tmp; |
| 141 | } |
| 142 | %} |
| 143 | |
| 144 | <INITIAL,ID>{ID}({ID}|"%")* { yylval.s = unique(yytext); |
| 145 | return WORD; } |
| 146 | |
| 147 | <PAT>{PAT}* { BEGIN(ID); |
| 148 | yylval.s = stralloc(yytext); |
| 149 | return PATTERN; } |
| 150 | |
| 151 | <BOM>"eeschema \("[^\n]* { hash = 0; |
| 152 | return BOM_EESCHEMA; } |
| 153 | <BOM>"|"[^\n]* { if (hash == 1) |
| 154 | process_bom_line(yytext); } |
| 155 | <BOM>"#End Cmp" { YY_FLUSH_BUFFER; |
| 156 | return 0; } |
| 157 | <BOM>#[^\n]* hash++; |
| 158 | <BOM>\n lineno++; |
| 159 | <BOM>. return *yytext; |
| 160 | |
| 161 | [<>=] { if (pattern) |
| 162 | BEGIN(PAT); |
| 163 | return *yytext; } |
| 164 | |
| 165 | "<=" { if (pattern) |
| 166 | BEGIN(PAT); |
| 167 | return TOK_LE; } |
| 168 | ">=" { if (pattern) |
| 169 | BEGIN(PAT); |
| 170 | return TOK_GE; } |
| 171 | |
| 172 | "//"[^\n]* ; |
| 173 | "/*"([^*]|("*"+([^*/])))*"*"+"/" { const char *s = yytext; |
| 174 | while (*s) |
| 175 | lineno += *s++ == '\n'; } |
| 176 | |
| 177 | [ \t] ; |
| 178 | \n { lineno++; |
| 179 | if (expose_nl) |
| 180 | return TOK_NL; } |
| 181 | ^#[^n]*\n lineno++; |
| 182 | |
| 183 | . return *yytext; |
| 184 | |
| 185 | %% |
| 186 | |
| 187 | |
| 188 | void yywarnf(const char *fmt, ...) |
| 189 | { |
| 190 | va_list ap; |
| 191 | |
| 192 | va_start(ap, fmt); |
| 193 | fprintf(stderr, "%s:%d: warning: ", file_name, lineno); |
| 194 | vfprintf(stderr, fmt, ap) ; |
| 195 | fprintf(stderr, "\n"); |
| 196 | va_end(ap); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | void yywarn(const char *s) |
| 201 | { |
| 202 | yywarnf("%s", s); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | void __attribute__((noreturn)) yyerrorf(const char *fmt, ...) |
| 207 | { |
| 208 | va_list ap; |
| 209 | |
| 210 | va_start(ap, fmt); |
| 211 | fprintf(stderr, "%s:%d: ", file_name, lineno); |
| 212 | vfprintf(stderr, fmt, ap) ; |
| 213 | fprintf(stderr, "\n"); |
| 214 | va_end(ap); |
| 215 | exit(1); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | void __attribute__((noreturn)) yyerror(const char *s) |
| 220 | { |
| 221 | yyerrorf("%s", s); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /* Define parse_kicad_bom here, so that we have access to BOM and INITIAL */ |
| 226 | |
| 227 | void parse_kicad_bom(const char *name) |
| 228 | { |
| 229 | BEGIN(BOM); |
| 230 | do_parse(name, START_BOM, 1, 0); |
| 231 | BEGIN(INITIAL); |
| 232 | } |
| 233 |
Branches:
master
