eu.mihosoft.vmf.vmftext.vmtemplates.model-unparser-match-alt.vm Maven / Gradle / Ivy
The newest version!
#*
* Copyright 2017-2018 Michael Hoffer . All rights reserved.
* Copyright 2017-2018 Goethe Center for Scientific Computing, University Frankfurt. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* If you use this software for scientific research then please cite the following publication(s):
*
* M. Hoffer, C. Poliwoda, & G. Wittum. (2013). Visual reflection library:
* a framework for declarative GUI programming on the Java platform.
* Computing and Visualization in Science, 2013, 16(4),
* 181–192. http://doi.org/10.1007/s00791-014-0230-y
*#
public static boolean match${altName}(String s) {
CharStream input = CharStreams.fromString(s);
${grammarName}ModelUnparserGrammarLexer lexer = new ${grammarName}ModelUnparserGrammarLexer(input);
lexer.removeErrorListener(org.antlr.v4.runtime.ConsoleErrorListener.INSTANCE);
CommonTokenStream tokens = new CommonTokenStream(lexer);
${grammarName}ModelUnparserGrammarParser parser = new ${grammarName}ModelUnparserGrammarParser(tokens);
parser.removeErrorListener(org.antlr.v4.runtime.ConsoleErrorListener.INSTANCE);
class ErrorListener implements org.antlr.v4.runtime.ANTLRErrorListener {
private boolean errors = false;
@Override
public void syntaxError(org.antlr.v4.runtime.Recognizer, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, org.antlr.v4.runtime.RecognitionException e) {
errors = true;
}
@Override
public void reportAmbiguity(org.antlr.v4.runtime.Parser recognizer, org.antlr.v4.runtime.dfa.DFA dfa, int startIndex, int stopIndex, boolean exact, java.util.BitSet ambigAlts, org.antlr.v4.runtime.atn.ATNConfigSet configs) {
}
@Override
public void reportAttemptingFullContext(org.antlr.v4.runtime.Parser recognizer, org.antlr.v4.runtime.dfa.DFA dfa, int startIndex, int stopIndex, java.util.BitSet conflictingAlts, org.antlr.v4.runtime.atn.ATNConfigSet configs) {
}
@Override
public void reportContextSensitivity(org.antlr.v4.runtime.Parser recognizer, org.antlr.v4.runtime.dfa.DFA dfa, int startIndex, int stopIndex, int prediction, org.antlr.v4.runtime.atn.ATNConfigSet configs) {
}
public boolean hasErrors() {
return this.errors;
}
}
ErrorListener l = new ErrorListener();
lexer.addErrorListener(l);
parser.addErrorListener(l);
// we cancel immediately if errors occur
parser.setErrorHandler(new BailErrorStrategy());
try {
parser.$Util.firstToLower(${altName})();
} catch(org.antlr.v4.runtime.misc.ParseCancellationException ex) {
return false;
} catch(Exception ex) {
return false;
}
// no errors encountered
return !l.hasErrors();
}