-
Notifications
You must be signed in to change notification settings - Fork 36
/
spec_tokenizer.l
103 lines (82 loc) · 2.48 KB
/
spec_tokenizer.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Copyright (C) 2011 Anders Sundman <[email protected]>
*
* This file is part of mfterm.
*
* mfterm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mfterm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mfterm. If not, see <http://www.gnu.org/licenses/>.
*/
%{
#include <stdio.h>
#include <stdarg.h>
#include "util.h"
#include "spec_syntax.h"
#include "libsp_a-spec_parser.h"
int colnum = 0;
#define YY_USER_ACTION { \
sp_lloc.first_line = sp_lineno; \
sp_lloc.first_column = colnum; \
colnum = colnum + sp_leng; \
sp_lloc.last_column=colnum; \
sp_lloc.last_line = sp_lineno; \
}
%}
%option yylineno bison-bridge bison-locations
ws [ \t]
nl [\r\n]
comment #[^\n]*
hex_digit [0-9a-fA-F]
hex_number 0x{hex_digit}+
dec_number [0-9]+
identifier [a-zA-Z][a-zA-Z0-9_]*
byte Byte(\[|{ws})
bit Bit(\[|{ws})
br [\[\]\{\}]
%%
{identifier} {
sp_lval.string = strdup(sp_text);
return IDENTIFIER;
}
{dec_number} {
sp_lval.string = strdup(sp_text);
return DEC_NUM;
}
{hex_number} {
sp_lval.string = strdup(sp_text);
return HEX_NUM;
}
{byte} { yyless(sp_leng - 1); return BYTE; }
{bit} { yyless(sp_leng - 1); return BIT; }
{br}|[\.-] { return sp_text[0]; }
{comment} {} // eat comments
{ws}+ {} // eat white space
{nl}+ { colnum = 0; } // eat new lines and reset column
. {
printf("Line: %d - Unrecognized input: %s\n", sp_lineno, sp_text);
return 0;
}
%%
// Report error, reset the line numbering and flush buffer
void sp_error(const char* s, ...) {
va_list ap;
va_start(ap, s);
sp_lerror(sp_lloc, s, ap);
sp_lineno = 1;
YY_FLUSH_BUFFER;
}
// Reset the line numbering and flush the buffer at the end of a file
int sp_wrap() {
sp_lineno = 1;
YY_FLUSH_BUFFER;
return 1;
}