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

scheme.lua (8209B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- Scheme LPeg lexer.
  3. -- Contributions by Murray Calavera.
  4. local lexer = require('lexer')
  5. local token, word_match = lexer.token, lexer.word_match
  6. local P, S = lpeg.P, lpeg.S
  7. local lex = lexer.new('scheme')
  8. -- Whitespace.
  9. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  10. -- Keywords.
  11. lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
  12. 'and', 'or', 'not', 'else',
  13. --
  14. 'library', 'define-library', 'export', 'include-library-declarations', 'cond-expand', 'import',
  15. 'rename', 'only', 'except', 'prefix', 'include', 'include-ci',
  16. --
  17. 'begin', 'case', 'case-lambda', 'cond', 'define', 'define-record-type', 'define-syntax',
  18. 'define-values', 'delay', 'delay-force', 'do', 'if', 'guard', 'lambda', 'let', 'let*',
  19. 'let*-values', 'let-syntax', 'let-values', 'letrec', 'letrec*', 'letrec-syntax', 'parameterize',
  20. 'quasiquote', 'quote', 'set!', 'unless', 'unquote', 'unquote-splicing', 'when',
  21. --
  22. 'define-macro', 'fluid-let'
  23. }))
  24. -- Functions.
  25. lex:add_rule('function', token(lexer.FUNCTION, word_match{
  26. '*', '+', '-', '/', '<', '<=', '=', '=>', '>', '>=', 'abs', 'append', 'apply', 'assoc', 'assq',
  27. 'assv', 'binary-port?', 'boolean=?', 'boolean?', 'bytevector', 'bytevector-append',
  28. 'bytevector-copy', 'bytevector-copy!', 'bytevector-length', 'bytevector-u8-ref',
  29. 'bytevector-u8-set!', 'bytevector?', 'caar', 'cadr', 'call-with-current-continuation',
  30. 'call-with-port', 'call-with-values', 'call/cc', 'car', 'cdar', 'cddr', 'cdr', 'ceiling',
  31. 'char->integer', 'char-ready?', 'char<=?', 'char<?', 'char=?', 'char>=?', 'char>?', 'char?',
  32. 'close-input-port', 'close-output-port', 'close-port', 'complex?', 'cons', 'current-error-port',
  33. 'current-input-port', 'current-output-port', 'denominator', 'dynamic-wind', 'eof-object',
  34. 'eof-object?', 'eq?', 'equal?', 'eqv?', 'error', 'error-object-irritants', 'error-object-message',
  35. 'error-object?', 'even?', 'exact', 'exact-integer-sqrt', 'exact-integer?', 'exact?', 'expt',
  36. 'features', 'file-error?', 'floor', 'floor-quotient', 'floor-remainder', 'floor/',
  37. 'flush-output-port', 'for-each', 'gcd', 'get-output-bytevector', 'get-output-string', 'inexact',
  38. 'inexact?', 'input-port-open?', 'input-port?', 'integer->char', 'integer?', 'lcm', 'length',
  39. 'list', 'list->string', 'list->vector', 'list-copy', 'list-ref', 'list-set!', 'list-tail',
  40. 'list?', 'make-bytevector', 'make-list', 'make-parameter', 'make-string', 'make-vector', 'map',
  41. 'max', 'member', 'memq', 'memv', 'min', 'modulo', 'negative?', 'newline', 'null?',
  42. 'number->string', 'number?', 'numerator', 'odd?', 'open-input-bytevector', 'open-input-string',
  43. 'open-output-bytevector', 'open-output-string', 'output-port-open?', 'output-port?', 'pair?',
  44. 'peek-char', 'peek-u8', 'port?', 'positive?', 'procedure?', 'quotient', 'raise',
  45. 'raise-continuable', 'rational?', 'rationalize', 'read-bytevector', 'read-bytevector!',
  46. 'read-char', 'read-error?', 'read-line', 'read-string', 'read-u8', 'real?', 'remainder',
  47. 'reverse', 'round', 'set-car!', 'set-cdr!', 'square', 'string', 'string->list', 'string->number',
  48. 'string->symbol', 'string->utf8', 'string->vector', 'string-append', 'string-copy',
  49. 'string-copy!', 'string-fill!', 'string-for-each', 'string-length', 'string-map', 'string-ref',
  50. 'string-set!', 'string<=?', 'string<?', 'string=?', 'string>=?', 'string>?', 'string?',
  51. 'substring', 'symbol->string', 'symbol=?', 'symbol?', 'syntax-error', 'syntax-rules',
  52. 'textual-port?', 'truncate', 'truncate-quotient', 'truncate-remainder', 'truncate/', 'u8-ready?',
  53. 'utf8->string', 'values', 'vector', 'vector->list', 'vector->string', 'vector-append',
  54. 'vector-copy', 'vector-copy!', 'vector-fill!', 'vector-for-each', 'vector-length', 'vector-map',
  55. 'vector-ref', 'vector-set!', 'vector?', 'with-exception-handler', 'write-bytevector',
  56. 'write-char', 'write-string', 'write-u8', 'zero?',
  57. --
  58. 'char-alphabetic?', 'char-ci<=?', 'char-ci<?', 'char-ci=?', 'char-ci>=?', 'char-ci>?',
  59. 'char-downcase', 'char-foldcase', 'char-lower-case?', 'char-numeric?', 'char-upcase',
  60. 'char-upper-case?', 'char-whitespace?', 'digit-value', 'string-ci<=?', 'string-ci<?',
  61. 'string-ci=?', 'string-ci>=?', 'string-ci>?', 'string-downcase', 'string-foldcase',
  62. 'string-upcase',
  63. --
  64. 'angle', 'imag-part', 'magnitude', 'make-polar', 'make-rectangular', 'real-part',
  65. --
  66. 'caaaar', 'caaadr', 'caaar', 'caadar', 'caaddr', 'caadr', 'cadaar', 'cadadr', 'cadar', 'caddar',
  67. 'cadddr', 'caddr', 'cdaaar', 'cdaadr', 'cdaar', 'cdadar', 'cdaddr', 'cdadr', 'cddaar', 'cddadr',
  68. 'cddar', 'cdddar', 'cddddr', 'cdddr',
  69. --
  70. 'environment', 'eval',
  71. --
  72. 'call-with-input-file', 'call-with-output-file', 'delete-file', 'file-exists?',
  73. 'open-binary-input-file', 'open-binary-output-file', 'open-input-file', 'open-output-file',
  74. 'with-input-from-file', 'with-output-to-file',
  75. --
  76. 'acos', 'asin', 'atan', 'cos', 'exp', 'finite?', 'infinite?', 'log', 'nan?', 'sin', 'sqrt', 'tan',
  77. --
  78. 'force', 'make-promise', 'promise?',
  79. --
  80. 'load',
  81. --
  82. 'command-line', 'emergency-exit', 'exit', 'get-environment-variable', 'get-environment-variables',
  83. --
  84. 'read',
  85. --
  86. 'interaction-environment',
  87. --
  88. 'current-jiffy', 'current-second', 'jiffies-per-second',
  89. --
  90. 'display', 'write', 'write-shared', 'write-simple',
  91. --
  92. 'syntax-case', 'er-macro-transformer', 'sc-macro-transformer', 'rsc-macro-transformer'
  93. }))
  94. -- Identifiers and symbols.
  95. local explicit_sign = S('+-')
  96. local initial = lexer.alpha + S('!$%&*/:<=>?@^_~')
  97. local subsequent = initial + lexer.digit + explicit_sign + '.'
  98. local sign_subsequent = initial + explicit_sign
  99. local dot_subsequent = sign_subsequent + '.'
  100. -- LuaFormatter off
  101. local peculiar_identifier =
  102. explicit_sign * '.' * dot_subsequent * subsequent^0 +
  103. explicit_sign * sign_subsequent * subsequent^0 +
  104. '.' * dot_subsequent * subsequent^0 +
  105. explicit_sign
  106. -- LuaFormatter on
  107. local ident = lexer.range('|') + initial * subsequent^0 + peculiar_identifier
  108. lex:add_rule('identifier', token(lexer.IDENTIFIER, ident))
  109. lex:add_rule('symbol', token(lexer.CLASS, "'" * ident))
  110. -- Strings.
  111. local character = '#\\' *
  112. (word_match('alarm backspace delete escape newline null return space tab') + 'x' * lexer.xdigit^1 +
  113. lexer.any)
  114. local dq_str = lexer.range('"')
  115. lex:add_rule('string', token(lexer.STRING, character + dq_str))
  116. -- Constants.
  117. lex:add_rule('constant', token(lexer.CONSTANT, word_match('#t #f #true #false')))
  118. -- Directives.
  119. lex:add_rule('directive', token(lexer.PREPROCESSOR, P('#!fold-case') + '#!no-fold-case'))
  120. -- Comments.
  121. local line_comment = lexer.to_eol(';')
  122. local block_comment = lexer.range('#|', '|#', false, false, true)
  123. local datum_comment = '#;' * lexer.space^0 * lexer.range('(', ')', false, true, true) *
  124. (lexer.any - lexer.space)^0
  125. lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment + datum_comment))
  126. -- Numbers.
  127. local radixes = {[2] = P('#b'), [8] = P('#o'), [10] = P('#d')^-1, [16] = P('#x')}
  128. local digits = {[2] = S('01'), [8] = lpeg.R('07'), [10] = lexer.digit, [16] = lexer.xdigit}
  129. local function num(r)
  130. local exactness = (P('#i') + '#e')^-1
  131. local radix, digit = radixes[r], digits[r]
  132. local prefix = radix * exactness + exactness * radix
  133. local suffix = ('e' * S('+-')^-1 * lexer.digit^1)^-1
  134. local infnan = S('+-') * word_match[[inf nan]] * '.0'
  135. -- LuaFormatter off
  136. local decimal = lexer.digit^1 * suffix +
  137. '.' * lexer.digit^1 * suffix +
  138. lexer.digit^1 * '.' * lexer.digit^0 * suffix
  139. local ureal = digit^1 * '/' * digit^1 +
  140. (r == 10 and decimal or P(false)) +
  141. digit^1
  142. local real = S('+-')^-1 * ureal + infnan
  143. local i = P('i')
  144. local complex = real * '@' * real +
  145. real * S('+-') * ureal^-1 * i +
  146. real * infnan * i +
  147. infnan * i +
  148. real +
  149. S('+-') * ureal^-1 * i
  150. -- LuaFormatter on
  151. return prefix * complex
  152. end
  153. lex:add_rule('number', token(lexer.NUMBER, num(2) + num(8) + num(10) + num(16)))
  154. -- Operators.
  155. lex:add_rule('operator', token(lexer.OPERATOR, P('#u8') + ',@' + S(".`'#(),")))
  156. -- Fold points.
  157. lex:add_fold_point(lexer.OPERATOR, '(', ')')
  158. lex:add_fold_point(lexer.COMMENT, '#|', '|#')
  159. lexer.property['scintillua.comment'] = ';'
  160. return lex