Implemented "Lexer nextSegmentUntil: terminatorChar" for String and Comment tokens...
[cslatevm.git] / etc / slate.lua
blob56fb03c2c54fcbe3d227f5e911288f411b8d5d1b
1 -- Copyright 2010 Alex Suraci
2 -- Slate LPeg Lexer
4 module(..., package.seeall)
5 local P, R, S = lpeg.P, lpeg.R, lpeg.S
7 local ws = token('whitespace', space^1)
9 -- comments
10 local comment = token('comment', delimited_range('"', '\\'))
12 -- strings
13 local string = token('string', delimited_range("'", '\\'))
15 -- chars
16 local char = token('char', '$' * (('\\' * P(1)) + P(1)))
18 -- numbers
19 local number = token('number', float + integer)
21 -- operators
22 local op = punct - S('()"\'') + S('\\[]]')
23 local operator = token('operator', op)
25 -- identifiers
26 local word = (alnum + S("._#"))^1
27 local identifier = token('identifier', word + (':' * word))
29 -- symbols
30 local symbol = token('symbol', '#' * word)
32 -- keywords
33 local keyword = token('slate_keyword', word * ':')
35 -- optional keywords
36 local optional_keyword = token('slate_optional_keyword', '&' * word * ':')
38 -- constructors
39 local constructor = token('type', upper * word)
41 function LoadTokens()
42 local slate = slate
43 add_token(slate, 'whitespace', ws)
44 add_token(slate, 'symbol', symbol)
45 add_token(slate, 'slate_keyword', keyword)
46 add_token(slate, 'slate_optional_keyword', optional_keyword)
47 add_token(slate, 'type', constructor)
48 add_token(slate, 'identifier', identifier)
49 add_token(slate, 'string', string)
50 add_token(slate, 'char', char)
51 add_token(slate, 'comment', comment)
52 add_token(slate, 'number', number)
53 add_token(slate, 'operator', operator)
54 add_token(slate, 'any_char', any_char)
55 end
57 function LoadStyles()
58 add_style('symbol', style_symbol)
59 add_style('slate_keyword', style_slate_keyword)
60 add_style('slate_optional_keyword', style_slate_keyword)
61 end