summaryrefslogtreecommitdiff
path: root/src/rlparse.lm
blob: f0003be0bba2ffbedbd8912d1be921c0891a825c (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
include 'ragel.lm'
include 'rlreduce.lm'

rl ident
	/( alpha | '_' ) ( alpha | digit | '_' )*/

rl number
	/ digit+ /

rl hex_number
	/ '0x' [0-9a-fA-F]+ /

rl hex_char
	/ '0x' [0-9a-fA-F]{2}  /

rl NL / '\n' /

rl c_comment 
	/ '/*' ( any | NL )* :>> '*/' /

rl cpp_comment
	/ '//' [^\n]* NL /

rl ruby_comment
	/ '#' [^\n]* NL /

rl s_literal
	/ "'" ([^'\\\n] | '\\' (any | NL))* "'" /

rl d_literal
	/ '"' ([^"\\] | NL | '\\' (any | NL))* '"' /

rl host_re_literal
	/ '/' ([^/\\] | NL | '\\' (any | NL))* '/' /

namespace inline
	lex 
		literal `fpc `fc  `fcurs `ftargs
			`fentry `fhold `fexec `fgoto `fnext
			`fcall `fret `fbreak `fncall `fnret `fnbreak

		token ident /ident/
		token number /digit+/
		token hex_number /'0x' [0-9a-fA-F]+/
		token dec_number /'0x' [0-9a-fA-F]+/

		token comment
			/ c_comment | cpp_comment /

		token string
			/ s_literal | d_literal /

		token whitespace
			/ ( [ \t] | NL )+ /

		literal 
			`{ `} `:: `* `, `( `) `;

		token var_ref
			/ "$" [a-zA-Z_][a-zA-Z_0-9]* /
			{
				if GblActionParams 
				{
					input->push( make_token(
							typeid<var_ref>, input->pull( match_length ) ) )
				}
				else
				{
					# Just pull one char. Don't consume the word because it may
					# be a keyword.
					input->push( make_token(
							typeid<c_any>, input->pull( 1 ) ) )
				}
			}

		token c_any
			/ any /
	end
end

namespace host
	lex 
		literal `%%{

		token close_inc /'}--%%'/
		{
			input->push( make_token( typeid<close_inc>, input->pull( match_length ) ) )
			restoreGlobals()
		}

		token close_imp /'}++%%'/
		{
			input->push( make_token( typeid<close_imp>, input->pull( match_length ) ) )
			restoreGlobals()
		}

		token slr / '%%' [^{] [^\n]* '\n' /
		{
			# Translates single line to multi-line
			input->pull( 2 )
			R: str = input->pull( match_length - 3 )
			input->push( "}%%" )
			input->push( R )
			input->push( "%%{" )
		}

		rl NL / '\n' /

		rl s_literal
			/ "'" ([^'\\\n] | '\\' (any | NL))* "'" /

		rl d_literal
			/ '"' ([^"\\] | NL | '\\' (any | NL))* '"' /

		literal `define `=

		token ident /ident "'"?/
		token number /digit+/
		token hex_number /'0x' [0-9a-fA-F]+/

		token comment
			/ c_comment | cpp_comment /

		token string
			/ s_literal | d_literal /

		token whitespace
			/ ( [ \t] | NL )+ /

		token c_any / any /
	end

	def tok
		[`define whitespace ident whitespace? number]  :ImportDefNum
	|	[`define whitespace ident whitespace? string]  :ImportDefStr
	|	[ident whitespace? `= whitespace? number]      :ImportAssignNum
	|	[ident whitespace? `= whitespace? string]      :ImportAssignStr
	|	[`define]              :Def
	|	[`=]                   :Eq
	|	[ident] :Ident
	|	[number] :Number
	|	[hex_number] :HexNumber
	|	[comment] :Comment
	|	[string] :String
	|	[whitespace] :Whitespace
	|	[c_any] :Any
end

reduction TopLevel

	# Pass Through.
	# def tok
	#	[`define ident number] :Def1
	# |	[`define ident string] :Def2
	# |	[ident `= number]      :Ass1
	# |	[ident `= string]      :Ass2
	#	[`define whitespace ident whitespace? number]  :ImportDefNum
	# |	[`define whitespace ident whitespace? string]  :ImportDefStr
	# |	[ident whitespace? `= whitespace? number]      :ImportAssignNum
	# |	[ident whitespace? `= whitespace? string]      :ImportAssignStr
	# |	[`define]              :Def
	# |	[`=]                   :Eq
	# |	[ident] :Ident
	# |	[number] :Number
	# |	[hex_number] :HexNumber
	# |	[comment] :Comment
	# |	[string] :String
	# |	[whitespace] :Whitespace
	# |	[c_any] :Any

	host::tok  :ImportDefNum
	{
		if ( isImport )
		{
			Literal *lit = new Literal( @number,
					false /* $number->neg */, $number->data,
					$number->length, Literal::Number );

			string name( $ident->data, $ident->length );
			import( @ident, name, lit );
		}
	}

	host::tok  :ImportDefStr
	{
		if ( isImport )
		{
			Literal *lit = new Literal( @string, false,
					$string->data, $string->length, Literal::LitString );
			string name( $ident->data, $ident->length );
			import( @ident, name, lit );
		}
	}

	host::tok  :ImportAssignNum
	{
		if ( isImport )
		{
			Literal *lit = new Literal( @number,
					false /*$number->neg */, $number->data,
					$number->length, Literal::Number );
			string name( $ident->data, $ident->length );
			import( @ident, name, lit );
		}
	}

	host::tok  :ImportAssignStr
	{
		if ( isImport )
		{
			Literal *lit = new Literal( @string, false,
					$string->data, $string->length, Literal::LitString );

			string name( $ident->data, $ident->length );
			import( @ident, name, lit );
		}
	}

end


export RagelError: str

# File name. The open is expected to succeed. It is tested before the colm
# program is called.
A: list_el<str> = argv->pop_head_el()
GblFileName = A->value

# Remaining items are include paths.
while ( argv->length > 0 ) {
	A = argv->pop_head_el()
	GblIncludePaths->push_tail_el( A )
}

Stream: stream = open( GblFileName, "r" )
reduce TopLevel start[ Stream ]
RagelError = error