summaryrefslogtreecommitdiff
path: root/grammar/go/go.lm
blob: 0ab8f7fd5e46d6640c321af5d0fe75d09d0f44cc (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
include 'utf8.lm'

token BOM / 0xEF 0xBB 0xBF /

lex
	#
	# Definitions.
	#
	rl newline / 0x0A /

	rl valid_utf8 /
		0x00 .. 0x7F |
		0xC0 .. 0xDF any |
		0xE0 .. 0xEF any any |
		0xF0 .. 0xF7 any any any
	/

	rl unicode_char  / valid_utf8 - 0x0A /

	rl letter        / unicode_letter | '_' /
	rl binary_digit  / '0' | '1' /
	rl octal_digit   / '0' .. '7' /
	rl decimal_digit / '0' .. '9' /
	rl hex_digit     / '0' .. '9' | 'A' .. 'F' | 'a' .. 'f' /

	#
	# Tokens
	#

	literal
		`+    `&     `+=    `&=     `&&    `==    `!=    `(    `)
		`-    `|     `-=    `|=     `||    `<     `<=    `[    `]
		`*    `^     `*=    `^=     `<-    `>     `>=    `{    `}
		`/    `<<    `/=    `<<=    `++    `=     `:=    `,    #;
		`%    `>>    `%=    `>>=    `--    `!     `...   `.    `:
			  `&^           `&^=

	token SEMI /';'/

	literal
		`break        `default      `func         `interface    `select
		`case         `defer        `go           `map          `struct
		`chan         `else         `goto         `package      `switch
		`const        `fallthrough  `if           `range        `type
		`continue     `for          `import       `return       `var

	token id
		/ letter ( letter | unicode_digit )* /

	#
	# Non-float numbers
	#
	
	rl binary_digits  / binary_digit ( '_'? binary_digit )* /
	rl octal_digits   / octal_digit ( '_'? octal_digit )* /
	rl decimal_digits / decimal_digit ( '_'? decimal_digit )* /
	rl hex_digits     / hex_digit ( '_'? hex_digit )* /

	token binary_lit     / '0' ( 'b' | 'B' ) '_'? binary_digits /
	token octal_lit      / '0' ( 'o' | 'O' )? '_'? octal_digits /
	token decimal_lit    / '0' | ( '1' .. '9' ) ( '_'? decimal_digits )? /
	token hex_lit        / '0' ( 'x' | 'X' ) '_'? hex_digits /

	rl int_lit
		/ decimal_lit | binary_lit | octal_lit | hex_lit /

	def int_lit
		[decimal_lit] | [binary_lit] | [octal_lit] | [hex_lit]

	rl decimal_exponent  / ( 'e' | 'E' ) ( '+' | '-' )? decimal_digits /

	#
	# Floats
	#
	token decimal_float_lit /
		decimal_digits '.' decimal_digits? decimal_exponent? |
		decimal_digits decimal_exponent |
		'.' decimal_digits decimal_exponent? /

	rl hex_mantissa
		/ '_'? hex_digits '.' hex_digits? | '_'? hex_digits | '.' hex_digits /
	rl hex_exponent
		/ ( 'p' | 'P' ) ( '+' | '-' )? decimal_digits /

	token hex_float_lit
		/ '0' ( 'x' | 'X' ) hex_mantissa hex_exponent /

	rl float_lit
		/ decimal_float_lit | hex_float_lit /

	def float_lit
		[decimal_float_lit] | [hex_float_lit]
	
	#
	# Imaginary
	#
	token imaginary_lit / 
		( decimal_digits | int_lit | float_lit ) 'i' /
	
	#
	# Rune literals
	#


	rl escaped_char
		/ '\\' ( 'a' | 'b' | 'f' | 'n' | 'r' | 't' | 'v' | '\\' | "'" | '"' ) /

	rl octal_byte_value / '\\' octal_digit octal_digit octal_digit /
	rl hex_byte_value   / '\\' 'x' hex_digit hex_digit /
	rl little_u_value   / '\\' 'u' hex_digit hex_digit hex_digit hex_digit /
	rl big_u_value      / '\\' 'U' hex_digit hex_digit hex_digit hex_digit
							   hex_digit hex_digit hex_digit hex_digit /

	rl byte_value       / octal_byte_value | hex_byte_value /
	rl unicode_value    / unicode_char | little_u_value | big_u_value | escaped_char /

	token rune_lit / "'" ( unicode_value | byte_value ) "'" /

	#
	# String literals
	#
	rl raw_string_lit         / "`" ( ( unicode_char | newline ) - '`' )* "`" /
	rl interpreted_string_lit / '"' ( ( unicode_value | byte_value ) - '"' )* '"' /
	token string_lit          / raw_string_lit | interpreted_string_lit /

	#
	# Comments
	#
	rl line_comment
		/ '//' [^\n]* '\n' /
	
	rl general_comment
		/ '/*' any* :> '*/'/

	rl pre_insert_semi /
		( id -
			'break'     - 'default'     - 'func'      - 'interface' - 'select'
			'case'      - 'defer'       - 'go'        - 'map'       - 'struct'
			'chan'      - 'else'        - 'goto'      - 'package'   - 'switch'
			'const'     - 'fallthrough' - 'if'        - 'range'     - 'type'
			'continue'  - 'for'         - 'import'    - 'return'    - 'var' ) |
		int_lit |
		float_lit |
		imaginary_lit |
		rune_lit |
		string_lit
	/

	#
	# Semi-colons
	#
	token insert_semi /
		pre_insert_semi
		( [ \t]+ | general_comment )*
		( '/*' [^\n]* | line_comment | '\n'  )
	/
	{
		parse BA: break_apart::break_apart[match_text]

		Prefix: str = input->pull( BA.pre_semi.data.length )
		input->push( make_token( typeid<SEMI>, ';' ) )
		input->push( Prefix )
	}

	ignore /line_comment/
	ignore /general_comment/
	ignore /[ \t\n\r]+/
end

namespace break_apart
	lex
		token pre_semi / pre_insert_semi /
		ignore /line_comment/
		ignore /'/*' any*/
		ignore /[ \t\n\r]+/
	end

	def break_apart
		[pre_semi]
end

def item
	[id]
|	[int_lit]
|	[float_lit]
|	[imaginary_lit]
|	[rune_lit]
|	[string_lit]

def stmt
	[item+ SEMI]

def program
	[BOM? stmt*]