vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
dot.lua
(2050B)
1 -- Copyright 2006-2025 Brian "Sir Alaran" Schott. See LICENSE.
2 -- Dot LPeg lexer.
3 -- Based off of lexer code by Mitchell.
4
5 local lexer = require('lexer')
6 local token, word_match = lexer.token, lexer.word_match
7 local P, S = lpeg.P, lpeg.S
8
9 local lex = lexer.new('dot')
10
11 -- Whitespace.
12 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
13
14 -- Keywords.
15 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
16 'graph', 'node', 'edge', 'digraph', 'fontsize', 'rankdir', 'fontname', 'shape', 'label',
17 'arrowhead', 'arrowtail', 'arrowsize', 'color', 'comment', 'constraint', 'decorate', 'dir',
18 'headlabel', 'headport', 'headURL', 'labelangle', 'labeldistance', 'labelfloat', 'labelfontcolor',
19 'labelfontname', 'labelfontsize', 'layer', 'lhead', 'ltail', 'minlen', 'samehead', 'sametail',
20 'style', 'taillabel', 'tailport', 'tailURL', 'weight', 'subgraph'
21 }))
22
23 -- Types.
24 lex:add_rule('type', token(lexer.TYPE, word_match{
25 ' box', 'polygon', 'ellipse', 'circle', 'point', 'egg', 'triangle', 'plaintext', 'diamond',
26 'trapezium', 'parallelogram', 'house', 'pentagon', 'hexagon', 'septagon', 'octagon',
27 'doublecircle', 'doubleoctagon', 'tripleoctagon', 'invtriangle', 'invtrapezium', 'invhouse',
28 'Mdiamond', 'Msquare', 'Mcircle', 'rect', 'rectangle', 'none', 'note', 'tab', 'folder', 'box3d',
29 'record'
30 }))
31
32 -- Identifiers.
33 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
34
35 -- Strings.
36 local sq_str = lexer.range("'")
37 local dq_str = lexer.range('"')
38 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
39
40 -- Comments.
41 local line_comment = lexer.to_eol('//', true)
42 local block_comment = lexer.range('/*', '*/')
43 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
44
45 -- Numbers.
46 lex:add_rule('number', token(lexer.NUMBER, lexer.dec_num + lexer.float))
47
48 -- Operators.
49 lex:add_rule('operator', token(lexer.OPERATOR, S('->()[]{};')))
50
51 -- Fold points.
52 lex:add_fold_point(lexer.OPERATOR, '{', '}')
53 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
54
55 lexer.property['scintillua.comment'] = '//'
56
57 return lex