logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

fortran.lua (3879B)


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