-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathscanner.l
109 lines (90 loc) · 3.82 KB
/
scanner.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
104
105
106
107
108
109
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Krzysztof Narkiewicz <[email protected]>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
%{
#include <iostream>
#include <cstdlib>
#include "scanner.h"
#include "interpreter.h"
#include "parser.hpp"
#include "location.hh"
using namespace std;
// Original yyterminate() macro returns int. Since we're using Bison 3 variants
// as tokens, we must redefine it to change type from `int` to `Parser::semantic_type`
#define yyterminate() EzAquarii::Parser::make_END(EzAquarii::location());
// This will track current scanner location.
// Action is called when length of the token is known.
#define YY_USER_ACTION m_driver.increaseLocation(yyleng);
// !!!WARNING!!!
// Location API is used, but the location is not initialized, 'cause I'm lazy. When making
// a token with make_{something} method you can pass detailed token location. Current location
// is accessible with m_driver.location() method. All puzzle elements are there - just
// pass location value in every action code block below. I'm going to waste more time writing
// this excuse than putting this boilerplate below...
//
// Location class can be found in location.hh and posistion.hh files. It's just a bit too much
// boilerplate for this small example. Bummer.
%}
%option nodefault
%option noyywrap
%option c++
%option yyclass="Scanner"
%option prefix="EzAquarii_"
%%
[a-z]+ {
cout << "Scanner: identifier [" << yytext << "]" << endl;
return EzAquarii::Parser::make_STRING(yytext, EzAquarii::location( /* put location data here if you want */ ));
}
\( {
cout << "Scanner: '('" << endl;
return EzAquarii::Parser::make_LEFTPAR(EzAquarii::location());
}
\) {
cout << "Scanner: ')'" << endl;
return EzAquarii::Parser::make_RIGHTPAR(EzAquarii::location());
}
; {
cout << "Scanner: ';'" << endl;
return EzAquarii::Parser::make_SEMICOLON(EzAquarii::location());
}
, {
cout << "Scanner: ','" << endl;
return EzAquarii::Parser::make_COMMA(EzAquarii::location());
}
[\n\t ] {
//cout << "Scanner: whitechar (ignored)" << endl;
}
[1-9][0-9]* {
cout << "Scanner: decimal number: " << yytext << endl;
uint64_t number = strtoull(yytext, 0, 10);
return EzAquarii::Parser::make_NUMBER(number, EzAquarii::location());
}
. {
cout << "Scanner: unknown character [" << yytext << "]" << endl;
}
<<EOF>> { return yyterminate(); }
%%