лексер с двоеточиями

  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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
$EXTERN Add, Sub, Put, Get, Open, Exit, Arg, Prout, Symb, Append;
$ENTRY Lexer {
e.Arg = <DoLexer <GetSymbol <LoadFile e.Arg> (0)(1)>>;
}
$ENTRY FnLexer {
e.Arg = <Lexer e.Arg>;
}
Inc {
s.Arg = <Add 1 s.Arg>;
}
Dec {
s.Arg = <Sub s.Arg 1>;
}
DoLexer {
(s.Space Space s.Line s.Col)e.Tail = <DoLexer e.Tail>;
(';' Other s.Line s.Col)e.Tail = ((Semicolon (s.Line s.Col) ';') : <DoLexer e.Tail>);
(':' Other s.Line s.Col)e.Tail = ((Colon (s.Line s.Col) ':') : <DoLexer e.Tail>);
('<' Other s.Line s.Col)e.Tail = ((OpenCall (s.Line s.Col) '<') : <DoLexer e.Tail>);
('>' Other s.Line s.Col)e.Tail = ((CloseCall (s.Line s.Col) '>') : <DoLexer e.Tail>);
('(' Other s.Line s.Col)e.Tail = ((OpenBracket (s.Line s.Col) '(') : <DoLexer e.Tail>);
(')' Other s.Line s.Col)e.Tail = ((CloseBracket (s.Line s.Col) ')') : <DoLexer e.Tail>);
('{' Other s.Line s.Col)e.Tail = ((OpenBlock (s.Line s.Col) '{') : <DoLexer e.Tail>);
('}' Other s.Line s.Col)e.Tail = ((CloseBlock (s.Line s.Col) '}') : <DoLexer e.Tail>);
('=' Other s.Line s.Col)e.Tail = ((Assign (s.Line s.Col) '=') : <DoLexer e.Tail>);
('&' Other s.Line s.Col)e.Tail = ((Ref (s.Line s.Col) '&') : <DoLexer e.Tail>);
(',' Other s.Line s.Col) e.Tail = ((Comma (s.Line s.Col) ',') : <DoLexer e.Tail>);
(0 EOF s.Line s.Col)e.Tail = (EOF (s.Line s.Col) '!');
('e' LoLetter s.Line s.Col) ('.' Other s.LineDot s.ColDot) e.Tail =
<ReadVariable 'e' s.Line s.Col () e.Tail>;
('s' LoLetter s.Line s.Col) ('.' Other s.LineDot s.ColDot) e.Tail =
<ReadVariable 's' s.Line s.Col () e.Tail>;
('t' LoLetter s.Line s.Col) ('.' Other s.LineDot s.ColDot) e.Tail =
<ReadVariable 't' s.Line s.Col () e.Tail>;
('$' Other s.Line s.Col) e.Tail =
<ReadDirective s.Line s.Col ('$') e.Tail>;
(s.Char Digit s.Line s.Col)e.Tail =
<ReadNumber s.Line s.Col (s.Char) e.Tail>;
(s.Char UpLetter s.Line s.Col) e.Tail =
<ReadName s.Line s.Col (s.Char) e.Tail>;
('/' Other s.Line s.Col) ('*' Other s.Line s.Col2) e.Tail =
<SkipComment e.Tail>;
('\'' Other s.Line s.Col) e.Tail = <ReadChars e.Tail>;
}
ReadVariable {
s.VarType s.StartVarLine s.StartVarCol (e.Name) (s.Letter UpLetter s.Line s.Col) e.Tail =
<ReadVariable
s.VarType s.StartVarLine s.StartVarCol (<PushBack (e.Name) s.Letter>) e.Tail
>;
s.VarType s.StartVarLine s.StartVarCol (e.Name) (s.Letter LoLetter s.Line s.Col) e.Tail =
<ReadVariable
s.VarType s.StartVarLine s.StartVarCol (<PushBack (e.Name) s.Letter>) e.Tail
>;
s.VarType s.StartVarLine s.StartVarCol (e.Name) (s.Letter Digit s.Line s.Col) e.Tail =
<ReadVariable
s.VarType s.StartVarLine s.StartVarCol (<PushBack (e.Name) s.Letter>) e.Tail
>;
s.VarType s.StartVarLine s.StartVarCol (e.Name) e.Tail =
(Variable (s.StartVarLine s.StartVarCol) s.VarType (e.Name)) <DoLexer e.Tail>;
}
ReadName {
s.StartVarLine s.StartVarCol (e.Name) (s.Letter UpLetter s.Line s.Col) e.Tail =
<ReadName s.StartVarLine s.StartVarCol (<PushBack (e.Name) s.Letter>) e.Tail>;
s.StartVarLine s.StartVarCol (e.Name) (s.Letter LoLetter s.Line s.Col) e.Tail =
<ReadName s.StartVarLine s.StartVarCol (<PushBack (e.Name) s.Letter>) e.Tail>;
s.StartVarLine s.StartVarCol (e.Name) (s.Letter Digit s.Line s.Col) e.Tail =
<ReadName s.StartVarLine s.StartVarCol (<PushBack (e.Name) s.Letter>) e.Tail>;
s.StartVarLine s.StartVarCol (e.Name) ('-' Other s.Line s.Col) e.Tail =
<ReadName s.StartVarLine s.StartVarCol (<PushBack (e.Name) '_'>) e.Tail>;
s.StartVarLine s.StartVarCol (e.Name) ('_' Other s.Line s.Col) e.Tail =
<ReadName s.StartVarLine s.StartVarCol (<PushBack (e.Name) '_'>) e.Tail>;
s.StartVarLine s.StartVarCol (e.Name) e.Tail =
(Name (s.StartVarLine s.StartVarCol) e.Name) <DoLexer e.Tail>;
}
ReadDirective {
s.StartVarLine s.StartVarCol (e.Name) (s.Letter UpLetter s.Line s.Col) e.Tail =
<ReadDirective s.StartVarLine s.StartVarCol (<PushBack (e.Name) s.Letter>) e.Tail>;
s.StartVarLine s.StartVarCol (e.Name) e.Tail =
((Directive (s.StartVarLine s.StartVarCol) e.Name) : <DoLexer e.Tail>);
}
ReadNumber {
s.StartVarLine s.StartVarCol (e.Name) (s.Letter Digit s.Line s.Col) e.Tail =
<ReadNumber s.StartVarLine s.StartVarCol (<PushBack (e.Name) s.Letter>) e.Tail>;
s.StartVarLine s.StartVarCol (e.Name) (s.Letter s.Type s.Line s.Col) e.Tail =
(Number (s.StartVarLine s.StartVarCol) e.Name) <DoLexer (s.Letter s.Type s.Line s.Col) e.Tail>;
}
SkipComment {
('*' Other s.Line s.Col) ('/' Other s.Line s.Col2) e.Tail = <DoLexer e.Tail>;
(0 EOF s.Line s.Col) = (Error (s.Line s.Col) 'Unclosed comment') (EOF (s.Line s.Col));
t.OtherChar e.Tail = <SkipComment e.Tail>;
}
PushBack {
(e.Name) s.Letter = e.Name s.Letter;
}
ReadChars {
('\'' Other s.Line s.Col) e.Tail = <DoLexer e.Tail>;
(0 EOF s.Line s.Col) e.Tail = (Error (s.Line s.Col) 'Unclosed quote at end of file') (EOF (s.Line s.Col));
('\\' Other s.Line s.Col) e.Tail = <ReadChars-HandleEscape e.Tail>;
('\n' Space s.Line s.Col) e.Tail = ((Error (s.Line s.Col) 'Unclosed quote at end of line') : <DoLexer e.Tail>);
(s.Char s.Class s.Line s.Col) e.Tail =
((Char (s.Line s.Col) s.Char) : <ReadChars e.Tail>);
}
ReadChars-HandleEscape {
('n' LoLetter s.Line s.Col) e.Tail = ((Char (s.Line s.Col) '\n') : <ReadChars e.Tail>);
('t' LoLetter s.Line s.Col) e.Tail = ((Char (s.Line s.Col) '\t') : <ReadChars e.Tail>);
('\\' Other s.Line s.Col) e.Tail = ((Char (s.Line s.Col) '\\') : <ReadChars e.Tail>);
('\'' Other s.Line s.Col) e.Tail = ((Char (s.Line s.Col) '\'') : <ReadChars e.Tail>);
/* ('x' LoLetter s.Line s.Col) e.Tail = <ReadChars-HandleEscape-HexCode e.Tail>; */
('\n' Space s.Line s.Col) e.Tail = <ReadChars e.Tail>;
(s.Other s.Class s.Line s.Col) e.Tail =
((Error (s.Line s.Col) 'Bad symbol in escape') : <ReadChars (s.Other s.Class s.Line s.Col) e.Tail>);
}
GetNumber {
(s.Char Digit s.Line s.Col)e.Tail = s.Char : <GetNumber e.Tail>;
(' ' Other s.Line s.Col)e.Tail = (s.Line s.Col)')'<DoLexer e.Tail>;
(s.Char s.Class s.Line s.Col)e.Tail = (s.Line s.Col)')'<DoLexer (s.Char s.Class s.Line s.Col)e.Tail>;
}
GetName {
(s.Char UpLetter s.Line s.Col)e.Tail = s.Char : <GetName e.Tail>;
(s.Char LoLetter s.Line s.Col)e.Tail = s.Char : <GetName e.Tail>;
(s.Char Digit s.Line s.Col)e.Tail = s.Char : <GetName e.Tail>;
('-' Other s.Line s.Col)e.Tail = '-' : <GetName e.Tail>;
(' ' Other s.Line s.Col)e.Tail = (s.Line s.Col)')'<DoLexer e.Tail>;
(s.Char s.Class s.Line s.Col)e.Tail = (s.Line s.Col)')'<DoLexer (s.Char s.Class s.Line s.Col)e.Tail>;
}
LoadFile {
/* пусто */ = <Prout 'Expected file name'> : <Exit 1>;
e.FileName =
<Open 'r' 1 e.FileName> : <DoLoadFile>;
}
DoLoadFile {
e.Chars 0 = e.Chars '\n' 0;
e.Chars = e.Chars '\n' <DoLoadFile <Get 1>>;
}
Class {
s.Char = <DoClass
s.Char
(UpLetter 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
(LoLetter 'abcdefghijklmnopqrstuvwxyz')
(Digit '1234567890')
(Space ' \t\n')
(EOF 0)
>;
}
DoClass {
s.Char (s.Class s.Char e.Tail) e.Classes = s.Class;
s.Char (s.Class s.OtherChar e.Tail)e.Classes = <DoClass s.Char (s.Class e.Tail) e.Classes>;
s.Char (s.Class) e.Classes = <DoClass s.Char e.Classes>;
s.Char = Other;
}
GetSymbol {
0 e.Text (s.Line)(s.Col) = (0 EOF s.Line s.Col);
'\n' e.Text(s.Line)(s.Col) = (('\n' Space s.Line s.Col) : <GetSymbol e.Text (<Inc s.Line>) (1)>);
s.Char e.Text (s.Line)(s.Col) = ((s.Char <Class s.Char> s.Line s.Col) : <GetSymbol e.Text (s.Line)(<Inc s.Col>)>);
}