1 --------------------------------------------------------------------------------
4 -- Copyright (c) 2010 Martin Morawetz
6 -- Permission is hereby granted, free of charge, to any person obtaining a copy
7 -- of this software and associated documentation files (the "Software"), to deal
8 -- in the Software without restriction, including without limitation the rights
9 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 -- copies of the Software, and to permit persons to whom the Software is
11 -- furnished to do so, subject to the following conditions:
13 -- The above copyright notice and this permission notice shall be included in
14 -- all copies or substantial portions of the Software.
16 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 --------------------------------------------------------------------------------
25 -- Based on lexer code from Mitchell mitchell.att.foicica.com.
27 local l
= require('lexer')
28 local token
, word_match
= l
.token
, l
.word_match
29 local P
, R
, S
= lpeg
.P
, lpeg
.R
, lpeg
.S
31 local M
= {_NAME
= 'chuck'}
34 local ws
= token(l
.WHITESPACE
, l
.space^
1)
37 local line_comment
= '//' * l
.nonnewline_esc^
0
38 local block_comment
= '/*' * (l
.any
- '*/')^
0 * P('*/')^
-1
39 local comment
= token(l
.COMMENT
, line_comment
+ block_comment
)
42 local sq_str
= P('L')^
-1 * l
.delimited_range("'", true)
43 local dq_str
= P('L')^
-1 * l
.delimited_range('"', true)
44 local string = token(l
.STRING
, sq_str
+ dq_str
)
47 local number = token(l
.NUMBER
, l
.float
+ l
.integer
)
50 local constant
= token(l
.CONSTANT
, word_match
{
52 'false', 'maybe', 'me', 'null', 'NULL', 'pi', 'true'
55 -- Special special value.
56 local now
= token('now', P('now'))
59 local time
= token('time', word_match
{
60 'samp', 'ms', 'second', 'minute', 'hour', 'day', 'week'
64 local keyword
= token(l
.KEYWORD
, word_match
{
65 -- Control structures.
66 'break', 'continue', 'else', 'for', 'if', 'repeat', 'return', 'switch',
68 -- Other chuck keywords.
69 'function', 'fun', 'spork', 'const', 'new'
73 local class
= token(l
.CLASS
, word_match
{
75 'class', 'extends', 'implements', 'interface', 'private', 'protected',
76 'public', 'pure', 'super', 'static', 'this'
80 local types
= token(l
.TYPE
, word_match
{
81 'float', 'int', 'time', 'dur', 'void', 'same'
85 local ugen
= token('ugen', word_match
{'dac', 'adc', 'blackhole'})
88 local identifier
= token(l
.IDENTIFIER
, l
.word
)
91 local operator
= token(l
.OPERATOR
, S('+-/*%<>!=^&|?~:;.()[]{}@'))
97 {'constant', constant
},
103 {'identifier', identifier
},
104 {'comment', comment
},
106 {'operator', operator
},
110 ugen
= l
.STYLE_CONSTANT
,
111 time
= l
.STYLE_NUMBER
,
112 now
= l
.STYLE_CONSTANT
..',bold'