vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
java.lua
(7701B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Java LPeg lexer.
3 -- Modified by Brian Schott.
4
5 local lexer = require('lexer')
6 local P, S = lpeg.P, lpeg.S
7
8 local lex = lexer.new(...)
9
10 -- Classes.
11 lex:add_rule('classdef', lex:tag(lexer.KEYWORD, 'class') * lex:get_rule('whitespace') *
12 lex:tag(lexer.CLASS, lexer.word))
13
14 -- Keywords.
15 lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
16
17 -- Functions.
18 local builtin_func = lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN))
19 local func = lex:tag(lexer.FUNCTION, lexer.word)
20 local method = lpeg.B('.') * lex:tag(lexer.FUNCTION_METHOD, lexer.word)
21 lex:add_rule('function', (builtin_func + method + func) * #(lexer.space^0 * '('))
22
23 -- Constants.
24 lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN) +
25 'System.' * lexer.word_match('err in out')))
26
27 -- Types.
28 lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
29
30 -- Identifiers.
31 lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
32
33 -- Strings.
34 local sq_str = lexer.range("'", true)
35 local dq_str = lexer.range('"', true)
36 lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str))
37
38 -- Comments.
39 local line_comment = lexer.to_eol('//', true)
40 local block_comment = lexer.range('/*', '*/')
41 lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))
42
43 -- Numbers.
44 lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number * S('LlFfDd')^-1))
45
46 -- Annotations.
47 lex:add_rule('annotation', lex:tag(lexer.ANNOTATION, '@' * lexer.word))
48
49 -- Operators.
50 lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}')))
51
52 -- Fold points.
53 lex:add_fold_point(lexer.OPERATOR, '{', '}')
54 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
55
56 -- Word lists.
57 lex:set_word_list(lexer.KEYWORD, {
58 'abstract', 'assert', 'break', 'case', 'catch', 'class', 'const', 'continue', 'default', 'do',
59 'else', 'enum', 'extends', 'final', 'finally', 'for', 'goto', 'if', 'implements', 'import',
60 'instanceof', 'interface', 'native', 'new', 'package', 'private', 'protected', 'public', 'return',
61 'static', 'strictfp', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient',
62 'try', 'while', 'volatile', --
63 'true', 'false', 'null' -- literals
64 })
65
66 lex:set_word_list(lexer.FUNCTION_BUILTIN, {
67 'clone', 'equals', 'finalize', 'getClass', 'hashCode', 'notify', 'notifyAll', 'toString', 'wait', --
68 'Boolean.compare', 'Boolean.getBoolean', 'Boolean.parseBoolean', 'Boolean.valueOf', --
69 'Byte.compare', 'Byte.decode', 'Byte.parseByte', 'Byte.valueOf', --
70 'Character.charCount', 'Character.codePointAt', 'Character.codePointBefore',
71 'Character.codePointCount', 'Character.compare', 'Character.digit', 'Character.forDigit',
72 'Character.getName', 'Character.getNumericValue', 'Character.getType', 'Character.isAlphabetic',
73 'Character.isDefined', 'Character.isDigit', 'Character.isIdentifierIgnorable',
74 'Character.isIdeographic', 'Character.isISOControl', 'Character.isJavaIdentifierPart',
75 'Character.isJavaIdentifierStart', 'Character.isLetter', 'Character.isLetterOrDigit',
76 'Character.isLowerCase', 'Character.isMirrored', 'Character.isSpaceChar',
77 'Character.isSupplementaryCodePoint', 'Character.isSurrogate', 'Character.isSurrogatePair',
78 'Character.isTitleCase', 'Character.isUnicodeIdentifierPart',
79 'Character.isUnicodeIdentifierStart', 'Character.isUpperCase', 'Character.isValidCodePoint',
80 'Character.isWhitespace', 'Character.offsetByCodePoints', 'Character.reverseBytes',
81 'Character.toChars', 'Character.toCodePoint', 'Character.toLowerCase', 'Character.toTitleCase',
82 'Character.toUpperCase', 'Character.valueOf', --
83 'Double.compare', 'Double.doubleToLongBits', 'Double.doubleToRawLongBits', 'Double.isInfinite',
84 'Double.longBitsToDouble', 'Double.parseDouble', 'Double.toHexString', 'Double.valueOf', --
85 'Integer.bitCount', 'Integer.compare', 'Integer.decode', 'Integer.getInteger',
86 'Integer.highestOneBit', 'Integer.lowestOneBit', 'Integer.numberOfLeadingZeros',
87 'Integer.numberOfTrailingZeros', 'Integer.parseInt', 'Integer.reverse', 'Integer.reverseBytes',
88 'Integer.rotateLeft', 'Integer.rotateRight', 'Integer.signum', 'Integer.toBinaryString',
89 'Integer.toHexString', 'Integer.toOctalString', 'Integer.valueOf', --
90 'Math.abs', 'Math.acos', 'Math.asin', 'Math.atan', 'Math.atan2', 'Math.cbrt', 'Math.ceil',
91 'Math.copySign', 'Math.cos', 'Math.cosh', 'Math.exp', 'Math.expm1', 'Math.floor',
92 'Math.getExponent', 'Math.hypot', 'Math.IEEEremainder', 'Math.log', 'Math.log10', 'Math.log1p',
93 'Math.max', 'Math.min', 'Math.nextAfter', 'Math.nextUp', 'Math.pow', 'Math.random', 'Math.rint',
94 'Math.round', 'Math.scalb', 'Math.signum', 'Math.sin', 'Math.sinh', 'Math.sqrt', 'Math.tan',
95 'Math.tanh', 'Math.toDegrees', 'Math.toRadians', 'Math.ulp', --
96 'Runtime.getRuntime', --
97 'String.copyValueOf', 'String.format', 'String.valueOf', --
98 'System.arraycopy', 'System.clearProperty', 'System.console', 'System.currentTimeMillis',
99 'System.exit', 'System.gc', 'System.getenv', 'System.getProperties', 'System.getProperty',
100 'System.getSecurityManager', 'System.identityHashCode', 'System.inheritedChannel',
101 'System.lineSeparator', 'System.load', 'System.loadLibrary', 'System.mapLibraryName',
102 'System.nanoTime', 'System.runFinalization', 'System.setErr', 'System.setIn', 'System.setOut',
103 'System.setProperties', 'System.setProperty', 'System.setSecurityManager', --
104 'Thread.activeCount', 'Thread.currentThread', 'Thread.dumpStack', 'Thread.enumerate',
105 'Thread.getAllStackTraces', 'Thread.getDefaultUncaughtExceptionHandler', 'Thread.holdsLock',
106 'Thread.interrupted', 'Thread.setDefaultUncaughtExceptionHandler', 'Thread.sleep', 'Thread.yield' --
107 })
108
109 lex:set_word_list(lexer.CONSTANT_BUILTIN, {
110 'Double.MAX_EXPONENT', 'Double.MAX_VALUE', 'Double.MIN_EXPONENT', 'Double.MIN_NORMAL',
111 'Double.MIN_VALUE', 'Double.NaN', 'Double.NEGATIVE_INFINITY', 'Double.POSITIVE_INFINITY', --
112 'Integer.MAX_VALUE', 'Integer.MIN_VALUE', --
113 'Math.E', 'Math.PI', --
114 'Thread.MAX_PRIORITY', 'Thread.MIN_PRIORITY', 'Thread.NORM_PRIORITY'
115 })
116
117 lex:set_word_list(lexer.TYPE, {
118 'boolean', 'byte', 'char', 'double', 'float', 'int', 'long', 'short', 'void', 'Boolean', 'Byte',
119 'Character', 'Class', 'Double', 'Enum', 'Float', 'Integer', 'Long', 'Object', 'Process',
120 'Runtime', 'Short', 'String', 'StringBuffer', 'StringBuilder', 'Thread', 'Throwable', 'Void',
121 -- Exceptions.
122 'ArithmeticException', 'ArrayIndexOutOfBoundsException', 'ArrayStoreException',
123 'ClassCastException', 'ClassNotFoundException', 'CloneNotSupportedException',
124 'EnumConstantNotPresentException', 'Exception', 'IllegalAccessException',
125 'IllegalArgumentException', 'IllegalMonitorStateException', 'IllegalStateException',
126 'IllegalThreadStateException', 'IndexOutOfBoundsException', 'InstantiationException',
127 'InterruptedException', 'NegativeArraySizeException', 'NoSuchFieldException',
128 'NoSuchMethodException', 'NullPointerException', 'NumberFormatException',
129 'ReflectiveOperationException', 'RuntimeException', 'SecurityException',
130 'StringIndexOutOfBoundsException', 'TypeNotPresentException', 'UnsupportedOperationException',
131 -- Errors.
132 'AbstractMethodError', 'AssertionError', 'BootstrapMethodError', 'ClassCircularityError',
133 'ClassFormatError', 'Error', 'ExceptionInInitializerError', 'IllegalAccessError',
134 'IncompatibleClassChangeError', 'InstantiationError', 'InternalError', 'LinkageError',
135 'NoClassDefFoundError', 'NoSuchFieldError', 'NoSuchMethodError', 'OutOfMemoryError',
136 'StackOverflowError', 'ThreadDeath', 'UnknownError', 'UnsatisfiedLinkError',
137 'UnsupportedClassVersionError', 'VerifyError', 'VirtualMachineError'
138 })
139
140 lexer.property['scintillua.comment'] = '//'
141
142 return lex