diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseParser.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseParser.java index 8d2523efa2..3618bbfbb1 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseParser.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseParser.java @@ -7,6 +7,8 @@ package net.sourceforge.pmd.lang.ast.impl.antlr4; import java.io.IOException; import java.io.Reader; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.Lexer; import net.sourceforge.pmd.lang.Parser; @@ -18,15 +20,14 @@ import net.sourceforge.pmd.lang.ast.RootNode; * Generic Antlr parser adapter for all Antlr parsers. This wraps a parser * generated by antlr, soo {@link PmdAntlrParserBase}. * - * @param

Type of the wrapped parser * @param Supertype of all nodes for the language, eg SwiftNode * @param Type of the root node */ public abstract class AntlrBaseParser< - P extends org.antlr.v4.runtime.Parser, I extends AntlrNode, N extends AntlrBaseInnerNode, - R extends AntlrBaseInnerNode & RootNode> implements Parser { + R extends AntlrBaseInnerNode & RootNode + > implements Parser { protected final ParserOptions parserOptions; @@ -41,18 +42,16 @@ public abstract class AntlrBaseParser< @Override public R parse(final String fileName, final Reader source) throws ParseException { + CharStream cs; try { - Lexer lexer = getLexer(source); - P parser = getParser(lexer); - return parse(parser); + cs = CharStreams.fromReader(source, fileName); } catch (final IOException e) { throw new ParseException(e); } + return parse(getLexer(cs)); } - protected abstract R parse(P parser); + protected abstract R parse(Lexer parser); - protected abstract Lexer getLexer(Reader source) throws IOException; - - protected abstract P getParser(Lexer lexer); + protected abstract Lexer getLexer(CharStream source); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrNameDictionary.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrNameDictionary.java index 473a2f5008..0648c755b2 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrNameDictionary.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrNameDictionary.java @@ -33,6 +33,7 @@ public class AntlrNameDictionary { for (int i = 0; i < xpathNames.length; i++) { String name = vocab.getSymbolicName(i); + if (name == null) { name = vocab.getLiteralName(i); @@ -43,6 +44,8 @@ public class AntlrNameDictionary { name = maybePunctName(name); } } + } else { + assert name.matches("[a-zA-Z][\\w_-]+"); // must be a valid xpath name } if (name == null) { name = String.valueOf(i); @@ -95,6 +98,7 @@ public class AntlrNameDictionary { case "@": return "at-symbol"; case "$": return "dollar"; + case "&": return "amp"; case "\\": return "backslash"; case "/": return "slash"; @@ -108,16 +112,16 @@ public class AntlrNameDictionary { case "<": return "lt"; case "<=": return "le"; - case "=": return "eq"; - case "==": return "double-eq"; - case "===": return "triple-eq"; - case "!=": return "not-eq"; - case ">>": return "double-gt"; case "<<": return "double-lt"; case ">>>": return "triple-gt"; case "<<<": return "triple-lt"; + case "=": return "eq"; + case "==": return "double-eq"; + case "===": return "triple-eq"; + case "!=": return "not-eq"; + case "*": return "star"; case "**": return "double-star"; diff --git a/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftParserAdapter.java b/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftParserAdapter.java index 4e8dd6c1d2..c15bb5c274 100644 --- a/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftParserAdapter.java +++ b/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftParserAdapter.java @@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.swift.ast; import java.io.IOException; import java.io.Reader; +import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.Lexer; @@ -18,25 +19,20 @@ import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrBaseParser; /** * Adapter for the SwiftParser. */ -public class SwiftParserAdapter extends AntlrBaseParser { +public class SwiftParserAdapter extends AntlrBaseParser { public SwiftParserAdapter(final ParserOptions parserOptions) { super(parserOptions); } @Override - protected SwiftFileNode parse(final SwiftParser parser) { + protected SwiftFileNode parse(final Lexer lexer) { + SwiftParser parser = new SwiftParser(new CommonTokenStream(lexer)); return new SwiftFileNode(parser.topLevel()); } @Override - protected Lexer getLexer(final Reader source) throws IOException { - return new SwiftLexer(CharStreams.fromReader(source)); + protected Lexer getLexer(final CharStream source) { + return new SwiftLexer(source); } - - @Override - protected SwiftParser getParser(final Lexer lexer) { - return new SwiftParser(new CommonTokenStream(lexer)); - } - }