1 -- Copyright 2015-2016 David B. Lamkins <david@lamkins.net>. See LICENSE.
2 -- pure LPeg lexer, see http://purelang.bitbucket.org/
4 local l
= require('lexer')
5 local token
, word_match
= l
.token
, l
.word_match
6 local P
, R
, S
= lpeg
.P
, lpeg
.R
, lpeg
.S
8 local M
= {_NAME
= 'pure'}
11 local ws
= token(l
.WHITESPACE
, l
.space^
1)
14 local line_comment
= '//' * l
.nonnewline^
0
15 local block_comment
= '/*' * (l
.any
- '*/')^
0 * P('*/')^
-1
16 local comment
= token(l
.COMMENT
, line_comment
+ block_comment
)
19 local string = token(l
.STRING
, l
.delimited_range('"', true))
22 local bin
= '0' * S('Bb') * S('01')^
1
23 local hex
= '0' * S('Xx') * (R('09') + R('af') + R('AF'))^
1
25 local int
= (bin
+ hex
+ dec
) * P('L')^
-1
26 local rad = P('.') - P('..')
27 local exp = (S('Ee') * S('+-')^
-1 * int
)^
-1
28 local flt
= int
* (rad * dec
)^
-1 * exp + int^
-1 * rad * dec
* exp
29 local number = token(l
.NUMBER
, flt
+ int
)
32 local keyword
= token(l
.KEYWORD
, word_match
{
33 'namespace', 'with', 'end', 'using', 'interface', 'extern', 'let', 'const',
34 'def', 'type', 'public', 'private', 'nonfix', 'outfix', 'infix', 'infixl',
35 'infixr', 'prefix', 'postfix', 'if', 'otherwise', 'when', 'case', 'of',
40 local identifier
= token(l
.IDENTIFIER
, l
.word
)
43 local punct
= S('+-/*%<>~!=^&|?~:;,.()[]{}@#$`\\\'')
45 local operator
= token(l
.OPERATOR
, dots
+ punct
)
48 local hashbang
= l
.starts_line('#!') * (l
.nonnewline
- P('//'))^
0
49 local pragma
= token(l
.PREPROCESSOR
, hashbang
)
57 {'operator', operator
},
58 {'identifier', identifier
},