vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
pascal.lua
(2737B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Pascal LPeg lexer.
3
4 local lexer = require('lexer')
5 local token, word_match = lexer.token, lexer.word_match
6 local P, S = lpeg.P, lpeg.S
7
8 local lex = lexer.new('pascal')
9
10 -- Whitespace.
11 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
12
13 -- Keywords.
14 lex:add_rule('keyword', token(lexer.KEYWORD, word_match({
15 'and', 'array', 'as', 'at', 'asm', 'begin', 'case', 'class', 'const', 'constructor', 'destructor',
16 'dispinterface', 'div', 'do', 'downto', 'else', 'end', 'except', 'exports', 'file', 'final',
17 'finalization', 'finally', 'for', 'function', 'goto', 'if', 'implementation', 'in', 'inherited',
18 'initialization', 'inline', 'interface', 'is', 'label', 'mod', 'not', 'object', 'of', 'on', 'or',
19 'out', 'packed', 'procedure', 'program', 'property', 'raise', 'record', 'repeat',
20 'resourcestring', 'set', 'sealed', 'shl', 'shr', 'static', 'string', 'then', 'threadvar', 'to',
21 'try', 'type', 'unit', 'unsafe', 'until', 'uses', 'var', 'while', 'with', 'xor', 'absolute',
22 'abstract', 'assembler', 'automated', 'cdecl', 'contains', 'default', 'deprecated', 'dispid',
23 'dynamic', 'export', 'external', 'far', 'forward', 'implements', 'index', 'library', 'local',
24 'message', 'name', 'namespaces', 'near', 'nodefault', 'overload', 'override', 'package', 'pascal',
25 'platform', 'private', 'protected', 'public', 'published', 'read', 'readonly', 'register',
26 'reintroduce', 'requires', 'resident', 'safecall', 'stdcall', 'stored', 'varargs', 'virtual',
27 'write', 'writeonly', --
28 'false', 'nil', 'self', 'true'
29 }, true)))
30
31 -- Functions.
32 lex:add_rule('function', token(lexer.FUNCTION, word_match({
33 'chr', 'ord', 'succ', 'pred', 'abs', 'round', 'trunc', 'sqr', 'sqrt', 'arctan', 'cos', 'sin',
34 'exp', 'ln', 'odd', 'eof', 'eoln'
35 }, true)))
36
37 -- Types.
38 lex:add_rule('type', token(lexer.TYPE, word_match({
39 'shortint', 'byte', 'char', 'smallint', 'integer', 'word', 'longint', 'cardinal', 'boolean',
40 'bytebool', 'wordbool', 'longbool', 'real', 'single', 'double', 'extended', 'comp', 'currency',
41 'pointer'
42 }, true)))
43
44 -- Strings.
45 lex:add_rule('string', token(lexer.STRING, S('uUrR')^-1 * lexer.range("'", true, false)))
46
47 -- Identifiers.
48 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
49
50 -- Comments.
51 local line_comment = lexer.to_eol('//', true)
52 local bblock_comment = lexer.range('{', '}')
53 local pblock_comment = lexer.range('(*', '*)')
54 lex:add_rule('comment', token(lexer.COMMENT, line_comment + bblock_comment + pblock_comment))
55
56 -- Numbers.
57 lex:add_rule('number', token(lexer.NUMBER, lexer.number * S('LlDdFf')^-1))
58
59 -- Operators.
60 lex:add_rule('operator', token(lexer.OPERATOR, S('.,;^@:=<>+-/*()[]')))
61
62 lexer.property['scintillua.comment'] = '//'
63
64 return lex