build: set version to 0.5
[vis.git] / lua / lexers / pure.lua
blob56f002d6bf03a6781e62ec69473003a3d01559a8
1 -- Copyright 2015-2017 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'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
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)
18 -- Strings.
19 local string = token(l.STRING, l.delimited_range('"', true))
21 -- Numbers.
22 local bin = '0' * S('Bb') * S('01')^1
23 local hex = '0' * S('Xx') * (R('09') + R('af') + R('AF'))^1
24 local dec = R('09')^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)
31 -- Keywords.
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',
36 'then', 'else'
39 -- Identifiers.
40 local identifier = token(l.IDENTIFIER, l.word)
42 -- Operators.
43 local punct = S('+-/*%<>~!=^&|?~:;,.()[]{}@#$`\\\'')
44 local dots = P('..')
45 local operator = token(l.OPERATOR, dots + punct)
47 -- Pragmas.
48 local hashbang = l.starts_line('#!') * (l.nonnewline - P('//'))^0
49 local pragma = token(l.PREPROCESSOR, hashbang)
51 M._rules = {
52 {'whitespace', ws},
53 {'comment', comment},
54 {'pragma', pragma},
55 {'keyword', keyword},
56 {'number', number},
57 {'operator', operator},
58 {'identifier', identifier},
59 {'string', string},
62 return M