Make lexer abstract

This commit is contained in:
Nik Everett 2017-01-19 11:41:50 -05:00
parent dbb4a2ca6c
commit bb83c283bb
3 changed files with 33 additions and 15 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<project name="ant-stuff"> <project name="ant-stuff">
<!-- <!--
grammar regeneration logic grammar regeneration logic
we do this with ant for several reasons: we do this with ant for several reasons:
* remove generated tabs for forbidden-apis * remove generated tabs for forbidden-apis
@ -9,7 +9,7 @@
* fix CRLF line endings for windows consistency * fix CRLF line endings for windows consistency
* ability to make classes package-private * ability to make classes package-private
* keeping in source code control is easier on IDEs * keeping in source code control is easier on IDEs
* regeneration should be rare, no reason to be religious about generated files * regeneration should be rare, no reason to be religious about generated files
* all logic already written and battle tested in lucene build * all logic already written and battle tested in lucene build
--> -->
<target name="regenerate" description="Regenerate antlr lexer and parser" depends="run-antlr"/> <target name="regenerate" description="Regenerate antlr lexer and parser" depends="run-antlr"/>
@ -136,6 +136,10 @@
<replaceregexp match="public ((interface|class) \Q@{grammar}\E\w+)" replace="\1" encoding="UTF-8"> <replaceregexp match="public ((interface|class) \Q@{grammar}\E\w+)" replace="\1" encoding="UTF-8">
<fileset refid="grammar.fileset"/> <fileset refid="grammar.fileset"/>
</replaceregexp> </replaceregexp>
<!-- make the lexer abstract -->
<replaceregexp match="(class \Q@{grammar}\ELexer)" replace="abstract \1" encoding="UTF-8">
<fileset refid="grammar.fileset"/>
</replaceregexp>
<!-- nuke timestamps/filenames in generated files --> <!-- nuke timestamps/filenames in generated files -->
<replaceregexp match="\Q// Generated from \E.*" replace="\/\/ ANTLR GENERATED CODE: DO NOT EDIT" encoding="UTF-8"> <replaceregexp match="\Q// Generated from \E.*" replace="\/\/ ANTLR GENERATED CODE: DO NOT EDIT" encoding="UTF-8">
<fileset refid="grammar.fileset"/> <fileset refid="grammar.fileset"/>

View File

@ -23,12 +23,19 @@ lexer grammar PainlessLexer;
} }
@members{ @members{
protected boolean isSimpleType(String name) { /**
throw new UnsupportedOperationException("Must be implemented in a subclass"); * Check against the current whitelist to determine whether a token is a type
} * or not. Called by the {@code TYPE} token defined in {@code PainlessLexer.g4}.
protected boolean slashIsRegex() { * See also
throw new UnsupportedOperationException("Must be implemented in a subclass"); * <a href="https://en.wikipedia.org/wiki/The_lexer_hack">The lexer hack</a>.
} */
protected abstract boolean isSimpleType(String name);
/**
* Is the preceding {@code /} a the beginning of a regex (true) or a division
* (false).
*/
protected abstract boolean slashIsRegex();
} }
WS: [ \t\n\r]+ -> skip; WS: [ \t\n\r]+ -> skip;

View File

@ -12,7 +12,7 @@ import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.misc.*; import org.antlr.v4.runtime.misc.*;
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) @SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
class PainlessLexer extends Lexer { abstract class PainlessLexer extends Lexer {
static { RuntimeMetaData.checkVersion("4.5.1", RuntimeMetaData.VERSION); } static { RuntimeMetaData.checkVersion("4.5.1", RuntimeMetaData.VERSION); }
protected static final DFA[] _decisionToDFA; protected static final DFA[] _decisionToDFA;
@ -105,12 +105,19 @@ class PainlessLexer extends Lexer {
} }
protected boolean isSimpleType(String name) { /**
throw new UnsupportedOperationException("Must be implemented in a subclass"); * Check against the current whitelist to determine whether a token is a type
} * or not. Called by the {@code TYPE} token defined in {@code PainlessLexer.g4}.
protected boolean slashIsRegex() { * See also
throw new UnsupportedOperationException("Must be implemented in a subclass"); * <a href="https://en.wikipedia.org/wiki/The_lexer_hack">The lexer hack</a>.
} */
protected abstract boolean isSimpleType(String name);
/**
* Is the preceding {@code /} a the beginning of a regex (true) or a division
* (false).
*/
protected abstract boolean slashIsRegex();
public PainlessLexer(CharStream input) { public PainlessLexer(CharStream input) {