Remove parser type parameter

This commit is contained in:
Clément Fournier
2020-04-24 02:23:30 +02:00
parent f779688cab
commit f0652dda76
3 changed files with 24 additions and 25 deletions

View File

@ -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 <P> Type of the wrapped parser
* @param <N> Supertype of all nodes for the language, eg SwiftNode
* @param <R> Type of the root node
*/
public abstract class AntlrBaseParser<
P extends org.antlr.v4.runtime.Parser,
I extends AntlrNode,
N extends AntlrBaseInnerNode<N, I>,
R extends AntlrBaseInnerNode<N, I> & RootNode> implements Parser {
R extends AntlrBaseInnerNode<N, I> & 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);
}

View File

@ -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";

View File

@ -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<SwiftParser, SwiftNode, SwiftInnerNode, SwiftFileNode> {
public class SwiftParserAdapter extends AntlrBaseParser<SwiftNode, SwiftInnerNode, SwiftFileNode> {
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));
}
}