build: set version to 0.5
[vis.git] / lua / lexers / faust.lua
blobd685c49017d3c1786e15fb88a88898e871ee3fd6
1 -- Copyright 2015-2017 David B. Lamkins <david@lamkins.net>. See LICENSE.
2 -- Faust LPeg lexer, see http://faust.grame.fr/
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 = 'faust'}
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 int = R('09')^1
23 local rad = P('.')
24 local exp = (P('e') * S('+-')^-1 * int)^-1
25 local flt = int * (rad * int)^-1 * exp + int^-1 * rad * int * exp
26 local number = token(l.NUMBER, flt + int)
28 -- Keywords.
29 local keyword = token(l.KEYWORD, word_match{
30 'declare', 'import', 'mdoctags', 'dependencies', 'distributed', 'inputs',
31 'outputs', 'par', 'seq', 'sum', 'prod', 'xor', 'with', 'environment',
32 'library', 'component', 'ffunction', 'fvariable', 'fconstant', 'int', 'float',
33 'case', 'waveform', 'h:', 'v:', 't:'
36 -- Identifiers.
37 local identifier = token(l.IDENTIFIER, l.word)
39 -- Operators.
40 local punct = S('+-/*%<>~!=^&|?~:;,.()[]{}@#$`\\\'')
41 local operator = token(l.OPERATOR, punct)
43 -- Pragmas.
44 local mdoc = P('<mdoc>') * (l.any - P('</mdoc>'))^0 * P('</mdoc>')
45 local pragma = token(l.PREPROCESSOR, mdoc)
47 M._rules = {
48 {'whitespace', ws},
49 {'comment', comment},
50 {'pragma', pragma},
51 {'keyword', keyword},
52 {'number', number},
53 {'operator', operator},
54 {'identifier', identifier},
55 {'string', string},
58 return M