From b4a7ace9118d29241686dd46ff4b19be15fd1a32 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Mon, 2 Sep 2013 18:20:58 -0700 Subject: [PATCH] got decent start --- .../Java/src/org/antlr/v4/runtime/Lexer.java | 8 ++ .../tree/ParseTreePatternErrorStrategy.java | 75 +++++++++++++++++++ .../runtime/tree/ParseTreePatternLexer.java | 57 ++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreePatternErrorStrategy.java create mode 100644 runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreePatternLexer.java diff --git a/runtime/Java/src/org/antlr/v4/runtime/Lexer.java b/runtime/Java/src/org/antlr/v4/runtime/Lexer.java index c50721995..cc322176c 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Lexer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Lexer.java @@ -188,6 +188,14 @@ public abstract class Lexer extends Recognizer } } + /** When parsing "x = ;" pattern, we use nextTokenOrRuleToken() not + * nextToken() so is converted to RULE token instead of tokenizing. + */ + public Token nextTokenOrRuleToken() { + //if ( '<' id '>' or special pattern ) return RULE token for id + return nextToken(); + } + /** Instruct the lexer to skip creating a token for current lexer rule * and look for another token. nextToken() knows to keep looking when * a lexer rule finishes with token set to SKIP_TOKEN. Recall that diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreePatternErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreePatternErrorStrategy.java new file mode 100644 index 000000000..97778246e --- /dev/null +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreePatternErrorStrategy.java @@ -0,0 +1,75 @@ +/* + * [The "BSD license"] + * Copyright (c) 2012 Terence Parr + * Copyright (c) 2012 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.antlr.v4.runtime.tree; + +import org.antlr.v4.runtime.DefaultErrorStrategy; +import org.antlr.v4.runtime.Parser; +import org.antlr.v4.runtime.RecognitionException; +import org.antlr.v4.runtime.Token; + +/** Alter response to errors in rules so it checks for special RULE tokens + * representing patterns like . + + catch (RecognitionException re) { + _localctx.exception = re; + // DO NOTHING IF RULE TOKEN + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + + if it's a rule token, it'll cause a mismatch or no viable alt error + immediately at start token of rule attempt. + + When parsing "x = ;" pattern, we use nextTokenOrRuleToken() not + nextToken() so is converted to RULE token instead of tokenizing. + */ + +public class ParseTreePatternErrorStrategy extends DefaultErrorStrategy { + + public boolean isRuleToken(Token t) { + return t.getType() == 33; + } + + @Override + public void reportError(Parser recognizer, RecognitionException e) { + if ( isRuleToken(e.getOffendingToken()) ) { + super.reportError(recognizer, e); + } + } + + @Override + public void recover(Parser recognizer, RecognitionException e) { + if ( isRuleToken(e.getOffendingToken()) ) { + super.recover(recognizer, e); + } + } +} + diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreePatternLexer.java b/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreePatternLexer.java new file mode 100644 index 000000000..c5e00722f --- /dev/null +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreePatternLexer.java @@ -0,0 +1,57 @@ +/* + * [The "BSD license"] + * Copyright (c) 2012 Terence Parr + * Copyright (c) 2012 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.antlr.v4.runtime.tree; + +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.atn.ATN; + +public class ParseTreePatternLexer extends Lexer { + @Override + public ATN getATN() { + return null; + } + + @Override + public String[] getRuleNames() { + return new String[0]; + } + + @Override + public String getGrammarFileName() { + return null; + } + + @Override + public Token nextToken() { + return super.nextToken(); + } +}