vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
fortran.lua
(3837B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Fortran 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('fortran')
9
10 -- Whitespace.
11 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
12
13 -- Comments.
14 local line_comment = lexer.to_eol(lexer.starts_line(S('CcDd!*')) + '!')
15 lex:add_rule('comment', token(lexer.COMMENT, line_comment))
16
17 -- Keywords.
18 lex:add_rule('keyword', token(lexer.KEYWORD, word_match({
19 'include', 'interface', 'program', 'module', 'subroutine', 'function', 'contains', 'use', 'call',
20 'return',
21 -- Statements.
22 'case', 'select', 'default', 'continue', 'cycle', 'do', 'while', 'else', 'if', 'elseif', 'then',
23 'elsewhere', 'end', 'endif', 'enddo', 'equivalence', 'external', 'forall', 'where', 'exit',
24 'goto', 'pause', 'save', 'stop',
25 -- Operators.
26 '.not.', '.and.', '.or.', '.xor.', '.eqv.', '.neqv.', '.eq.', '.ne.', '.gt.', '.ge.', '.lt.',
27 '.le.',
28 -- Logical.
29 '.false.', '.true.',
30 -- Attributes and other keywords.
31 'access', 'action', 'advance', 'assignment', 'block', 'entry', 'in', 'inout', 'intent', 'only',
32 'out', 'optional', 'pointer', 'precision', 'procedure', 'recursive', 'result', 'sequence', 'size',
33 'stat', 'target', 'type'
34 }, true)))
35
36 -- Functions.
37 lex:add_rule('function', token(lexer.FUNCTION, word_match({
38 -- I/O.
39 'backspace', 'close', 'endfile', 'inquire', 'open', 'print', 'read', 'rewind', 'write', 'format',
40 -- Type conversion utility and math.
41 'aimag', 'aint', 'amax0', 'amin0', 'anint', 'ceiling', 'cmplx', 'conjg', 'dble', 'dcmplx',
42 'dfloat', 'dim', 'dprod', 'float', 'floor', 'ifix', 'imag', 'int', 'logical', 'modulo', 'nint',
43 'real', 'sign', 'sngl', 'transfer', 'zext', 'abs', 'acos', 'aimag', 'aint', 'alog', 'alog10',
44 'amax0', 'amax1', 'amin0', 'amin1', 'amod', 'anint', 'asin', 'atan', 'atan2', 'cabs', 'ccos',
45 'char', 'clog', 'cmplx', 'conjg', 'cos', 'cosh', 'csin', 'csqrt', 'dabs', 'dacos', 'dasin',
46 'datan', 'datan2', 'dble', 'dcos', 'dcosh', 'ddim', 'dexp', 'dim', 'dint', 'dlog', 'dlog10',
47 'dmax1', 'dmin1', 'dmod', 'dnint', 'dprod', 'dreal', 'dsign', 'dsin', 'dsinh', 'dsqrt', 'dtan',
48 'dtanh', 'exp', 'float', 'iabs', 'ichar', 'idim', 'idint', 'idnint', 'ifix', 'index', 'int',
49 'isign', 'len', 'lge', 'lgt', 'lle', 'llt', 'log', 'log10', 'max', 'max0', 'max1', 'min', 'min0',
50 'min1', 'mod', 'nint', 'real', 'sign', 'sin', 'sinh', 'sngl', 'sqrt', 'tan', 'tanh',
51 -- Matrix math.
52 'matmul', 'transpose', 'reshape',
53 -- Other frequently used built-in statements.
54 'assign', 'nullify',
55 -- ISO C binding from Fortran 2003.
56 'c_sizeof', 'c_f_pointer', 'c_associated'
57 }, true)))
58
59 -- Types.
60 lex:add_rule('type', token(lexer.TYPE, word_match({
61 'implicit', 'explicit', 'none', 'data', 'parameter', 'allocate', 'allocatable', 'allocated',
62 'deallocate', 'integer', 'real', 'double', 'precision', 'complex', 'logical', 'character',
63 'dimension', 'kind',
64 -- ISO C binding from Fortran 2003
65 'bind', 'c_int', 'c_short', 'c_long', 'c_long_long', 'c_signed_char', 'c_size_t', 'c_int8_t',
66 'c_int16_t', 'c_int32_t', 'c_int64_t', 'c_int128_t', 'c_intptr_t', 'c_float', 'c_double',
67 'c_long_double', 'c_float128', 'c_float_complex', 'c_double_complex', 'c_long_double_complex',
68 'c_float128_complex', 'c_bool', 'c_char', 'c_null_char', 'c_new_line', 'c_null_ptr', 'c_funptr'
69 }, true)))
70
71 -- Numbers.
72 lex:add_rule('number', token(lexer.NUMBER, lexer.number * -lexer.alpha))
73
74 -- Identifiers.
75 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.alnum^1))
76
77 -- Strings.
78 local sq_str = lexer.range("'", true, false)
79 local dq_str = lexer.range('"', true, false)
80 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
81
82 -- Operators.
83 lex:add_rule('operator', token(lexer.OPERATOR, S('<>=&+-/*,()')))
84
85 lexer.property['scintillua.comment'] = '!'
86
87 return lex