本文整理匯總了Java中org.antlr.v4.runtime.CharStreams.fromString方法的典型用法代碼示例。如果您正苦於以下問題:Java CharStreams.fromString方法的具體用法?Java CharStreams.fromString怎麽用?Java CharStreams.fromString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.antlr.v4.runtime.CharStreams
的用法示例。
在下文中一共展示了CharStreams.fromString方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: parse
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
/**
* @throws InvalidQueryException if the given query contains invalid characters or is incomplete
*/
public SqlGrammarParser.SelectStatementContext parse(String query) {
log.trace("About to parse [{}]", query);
long now = currentTimeMillis();
CharStream input = CharStreams.fromString(query);
SqlGrammarLexer lexer = new SqlGrammarLexer(input);
TokenStream tokenStream = new CommonTokenStream(lexer);
SqlGrammarParser parser = new SqlGrammarParser(tokenStream);
parser.removeErrorListeners();
parser.addErrorListener(new RaiseExceptionErrorListener());
SqlGrammarParser.SelectStatementContext result = parser.selectStatement();
log.trace("Successfully parsed [{}] into [{}] in [{}ms]",
query, result.toStringTree(parser), currentTimeMillis() - now);
return result;
}
示例2: parseString
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
@Override
public SortedMap<Integer, String> parseString(String fieldString) {
CharStream is = CharStreams.fromString(fieldString);
DrawGrammarLexer lex = new DrawGrammarLexer(is);
CommonTokenStream tokens = new CommonTokenStream(lex);
DrawGrammarParser parser = new DrawGrammarParser(tokens);
palette = Palette.makeDefaultPalette("DarkSpectrum");
final SortedMap<Integer, String> resultMap;
try {
resultMap = parser.root().map;
} catch (RecognitionException | NullPointerException | StringIndexOutOfBoundsException | RasterFormatException re) {
//Something wrong with the parsing do not update.
return null;
}
return resultMap;
}
示例3: parse
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
public EObject parse(final String typeExpression, final Scope scope) {
final CharStream charStream = CharStreams.fromString(typeExpression);
final TypeExpressionLexer lexer = new TypeExpressionLexer(charStream);
final TokenStream tokenStream = new CommonTokenStream(lexer);
final TypeExpressionParser typeExpressionParser = new TypeExpressionParser(tokenStream);
lexer.removeErrorListeners();
typeExpressionParser.removeErrorListeners();
final ParserErrorCollector errorCollector = new ParserErrorCollector();
lexer.addErrorListener(errorCollector);
typeExpressionParser.addErrorListener(errorCollector);
final TypeExpressionParser.Type_exprContext typeExpr = typeExpressionParser.type_expr();
final EObject anyType = new TypeExpressionBuilder(scope, ARRAY_TYPE).visit(typeExpr);
scope.getResource().getErrors().addAll(errorCollector.getErrors());
return anyType;
}
示例4: evaluate
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
public static String evaluate(String templateString, Map<String, Object> environment) {
final Scanner s = new Scanner(templateString);
// TODO: This is a hack that fails if a string literal contains "}"
// TODO: It's because i couldn't be bothered nesting grammars.
s.useDelimiter("((?!\\\\)\\$\\{)|(})");
StringBuilder ret = new StringBuilder();
boolean isInsideExpr = templateString.startsWith("${");
while(s.hasNext()) {
final String str = s.next();
if (isInsideExpr) {
final JsLikeExpressionLexer lexer = new JsLikeExpressionLexer(CharStreams.fromString(str));
final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
final JsLikeExpressionParser parser = new JsLikeExpressionParser(tokenStream);
final TemplateStringEvaluatorVisitor visitor = new TemplateStringEvaluatorVisitor(environment);
ret.append(parser.expression().accept(visitor).toString());
} else {
ret.append(str);
}
isInsideExpr = !isInsideExpr;
}
return ret.toString();
}
示例5: parseUserAgent
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
private UserAgentContext parseUserAgent(UserAgent userAgent) {
String userAgentString = EvilManualUseragentStringHacks.fixIt(userAgent.getUserAgentString());
CodePointCharStream input = CharStreams.fromString(userAgentString);
UserAgentLexer lexer = new UserAgentLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
UserAgentParser parser = new UserAgentParser(tokens);
if (!verbose) {
lexer.removeErrorListeners();
parser.removeErrorListeners();
}
lexer.addErrorListener(userAgent);
parser.addErrorListener(userAgent);
return parser.userAgent();
}
示例6: compile
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
public static FilterPredicate compile(String predicateString){
predicateString = predicateString.trim();
if(predicateString.isEmpty()){
return FilterPredicate.empty();
}
PredicateCompileErrorListener errorListener = new PredicateCompileErrorListener(predicateString);
CharStream stream = CharStreams.fromString(predicateString);
PredicateLexer lexer = new PredicateLexer(stream);
lexer.removeErrorListener(ConsoleErrorListener.INSTANCE);
lexer.addErrorListener(errorListener);
CommonTokenStream tokens = new CommonTokenStream(lexer);
PredicateParser parser = new PredicateParser(tokens);
parser.removeErrorListener(ConsoleErrorListener.INSTANCE);
parser.addErrorListener(errorListener);
FilterPredicateVisitor filterPredicateVisitor = new FilterPredicateVisitor();
return filterPredicateVisitor.visit(parser.compilationUnit());
}
示例7: testExampleFieldCondition
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
@Test
public void testExampleFieldCondition() throws Exception {
ScoreLexer l = new ScoreLexer(CharStreams.fromString(expression));
ScoreParser p = new ScoreParser(new CommonTokenStream(l));
p.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) {
throw new IllegalStateException(String.format(
"Failed to parse at line %d position %d due to %s", line, charPositionInLine, msg), e);
}
});
ScoreTranslator visitor = new ScoreTranslator();
AnyExpressionContext ctx = p.anyExpression();
String text = visitor.visitAnyExpression(ctx);
System.out.println(text);
}
示例8: testExampleFieldCondition
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
@Test
public void testExampleFieldCondition() throws Exception {
ScoreLexer l = new ScoreLexer(CharStreams.fromString(fieldCondition));
ScoreParser p = new ScoreParser(new CommonTokenStream(l));
p.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) {
throw new IllegalStateException(String.format(
"Failed to parse at line %d position %d due to %s", line, charPositionInLine, msg), e);
}
});
ScoreBaseVisitor<Object> visitor = new ScoreBaseVisitor<>();
AnyExpressionContext ctx = p.anyExpression();
Object expression = visitor.visitAnyExpression(ctx);
//System.out.println(expression.getClass().getSimpleName());
}
示例9: test
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
@Test
public void test() {
final CharStream charStream = CharStreams.fromString("\r\nSELECT\r\n 1");
final TSqlLexer lexer = new TSqlLexer(charStream);
final CommonTokenStream stream = new CommonTokenStream(lexer);
stream.fill();
TSqlParser parser = new TSqlParser(stream);
ParseTree child = parser.tsql_file().getChild(0);
DefaultLinesProvider lines = new DefaultLinesProvider(stream);
int line = lines.getLine(new ParsedNode(child));
Assert.assertEquals(2, line);
}
示例10: parseKuduExpression
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
public void parseKuduExpression() throws Exception
{
KuduSQLExpressionLexer lexer = new KuduSQLExpressionLexer(CharStreams.fromString(sqlExpresssion));
CommonTokenStream tokens = new CommonTokenStream( lexer );
parser = new KuduSQLParser( tokens );
errorListener = parser.getKuduSQLExpressionErrorListener();
ParseTree parserTree = parser.kudusqlexpression();
if (!errorListener.isSyntaxError()) {
ParseTreeWalker parseTreeWalker = new ParseTreeWalker();
kuduSQLParseTreeListener = new KuduSQLParseTreeListener();
kuduSQLParseTreeListener.setColumnSchemaList(allColumnsForThisTable);
try {
parseTreeWalker.walk(kuduSQLParseTreeListener, parserTree);
} catch (Exception ex) {
LOG.error(" The supplied SQL expression could not be parsed because " + ex.getMessage(),ex);
errorListener.setSyntaxError(true);
}
} else {
LOG.error(" Syntax error present in the Kudu SQL expression. Hence not processing");
List<String> allRegisteredSyntaxErrors = errorListener.getListOfErrorMessages();
for (String syntaxErrorMessage : allRegisteredSyntaxErrors) {
LOG.error(" Error : " + syntaxErrorMessage + " in SQL expression \"" + sqlExpresssion + " \"");
}
}
}
示例11: read
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
@Nullable
@Override
public CharStream read(String name) {
for (Path prefix : pathList) {
Path path = prefix.resolve(name);
if (Files.isRegularFile(path)) {
try {
byte[] bytes = Files.readAllBytes(path);
String result = new String(bytes, StandardCharsets.UTF_8);
return CharStreams.fromString(result);
} catch (IOException e) {
LOGGER.trace("Could not read {}", path, e);
}
}
}
return null;
}
示例12: parseService
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
private Service parseService(String input) {
CharStream stream = CharStreams.fromString(input);
ProtoLexer lexer = new ProtoLexer(stream);
lexer.removeErrorListeners();
lexer.addErrorListener(TestUtils.ERROR_LISTENER);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
ProtoParser parser = new ProtoParser(tokenStream);
parser.removeErrorListeners();
parser.addErrorListener(TestUtils.ERROR_LISTENER);
ProtoContext context = new ProtoContext("test.proto");
ServiceParseListener serviceParseListener = new ServiceParseListener(tokenStream, context);
OptionParseListener optionParseListener = new OptionParseListener(tokenStream, context);
Proto proto = new Proto();
context.push(proto);
parser.addParseListener(serviceParseListener);
parser.addParseListener(optionParseListener);
parser.serviceBlock();
return proto.getServices().get(0);
}
示例13: parseEnumBlock
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
private Enum parseEnumBlock(String input) {
CharStream stream = CharStreams.fromString(input);
ProtoLexer lexer = new ProtoLexer(stream);
lexer.removeErrorListeners();
lexer.addErrorListener(TestUtils.ERROR_LISTENER);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
ProtoParser parser = new ProtoParser(tokenStream);
parser.setErrorHandler(new BailErrorStrategy());
parser.removeErrorListeners();
parser.addErrorListener(TestUtils.ERROR_LISTENER);
ProtoContext context = new ProtoContext("test.proto");
Proto proto = new Proto();
context.push(proto);
EnumParseListener enumParseListener = new EnumParseListener(tokenStream, context);
OptionParseListener optionParseListener = new OptionParseListener(tokenStream, context);
parser.addParseListener(enumParseListener);
parser.addParseListener(optionParseListener);
parser.enumBlock();
return proto.getEnums().get(0);
}
示例14: parseMessage
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
private Message parseMessage(String input) {
CharStream stream = CharStreams.fromString(input);
ProtoLexer lexer = new ProtoLexer(stream);
lexer.removeErrorListeners();
lexer.addErrorListener(TestUtils.ERROR_LISTENER);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
ProtoParser parser = new ProtoParser(tokenStream);
parser.removeErrorListeners();
parser.addErrorListener(TestUtils.ERROR_LISTENER);
ProtoContext context = new ProtoContext("test.proto");
MessageParseListener messageParseListener = new MessageParseListener(tokenStream, context);
OptionParseListener optionParseListener = new OptionParseListener(tokenStream, context);
Proto proto = new Proto();
context.push(proto);
parser.addParseListener(messageParseListener);
parser.addParseListener(optionParseListener);
parser.messageBlock();
return proto.getMessages().get(0);
}
示例15: parseProto
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
private Proto parseProto(String input) {
CharStream stream = CharStreams.fromString(input);
ProtoLexer lexer = new ProtoLexer(stream);
lexer.removeErrorListeners();
lexer.addErrorListener(TestUtils.ERROR_LISTENER);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
ProtoParser parser = new ProtoParser(tokenStream);
parser.removeErrorListeners();
parser.addErrorListener(TestUtils.ERROR_LISTENER);
ProtoContext context = new ProtoContext("test.proto");
ProtoParseListener protoParseListener = new ProtoParseListener(tokenStream, context);
OptionParseListener optionParseListener = new OptionParseListener(tokenStream, context);
parser.addParseListener(protoParseListener);
parser.addParseListener(optionParseListener);
parser.proto();
return context.getProto();
}