本文整理匯總了Java中org.antlr.v4.runtime.CharStreams類的典型用法代碼示例。如果您正苦於以下問題:Java CharStreams類的具體用法?Java CharStreams怎麽用?Java CharStreams使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CharStreams類屬於org.antlr.v4.runtime包,在下文中一共展示了CharStreams類的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: main
import org.antlr.v4.runtime.CharStreams; //導入依賴的package包/類
/**
* @param args the command line arguments
* @throws java.io.IOException
* @throws java.net.URISyntaxException
*/
public static void main(String[] args) throws IOException, URISyntaxException {
final String entryPoint;
final URL res;
switch (args.length) {
case 2:
res = Paths.get(args[0]).toUri().toURL();
entryPoint = args[1];
break;
default:
System.err.println("Supply two parameters in the following order:\n- file name of the main function\n- name of the main function\n\nFor example: hello.ct main:argc:argv");
return;
}
final CharStream inp = CharStreams.fromStream(res.openStream());
final GrammarLexer lex = new GrammarLexer(inp);
final TokenStream toks = new CommonTokenStream(lex);
final GrammarParser parser = new GrammarParser(toks);
System.out.println(new Translator(res.toURI()).generate(parser.program(), entryPoint));
}
示例4: link
import org.antlr.v4.runtime.CharStreams; //導入依賴的package包/類
/**
* Returns an ExecutableCode that can then be run by the processor. If the given code contains
* data section, linker will only generate memory addresses. Without loading process, referenced
* data will be unpredictable.
*
* @param code The code that will be linked.
*
* @return The executable code.
*
* @throws LabelError If an undefined label used or duplicate labels exist.
* @see Loader#load(Linker.ExecutableCode)
*/
public ExecutableCode link(String code) throws LabelError {
definedBranches.clear();
definedData.clear();
addressBook.clear();
secondPass = false;
this.code = parseCode(code);
AssemblerLexer lexer = new AssemblerLexer(CharStreams.fromString(code));
CommonTokenStream tokens = new CommonTokenStream(lexer);
AssemblerParser parser = new AssemblerParser(tokens);
AssemblerParser.ProgContext program = parser.prog();
visit(program);
secondPass = true;
visit(program);
List<Data> temp = new ArrayList<>(definedData.size());
definedData.forEach((label, data) -> {
temp.add(data);
});
return new ExecutableCode(this.code, temp);
}
示例5: 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;
}
示例6: 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();
}
示例7: 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();
}
示例8: 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());
}
示例9: 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);
}
示例10: 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());
}
示例11: combineInput
import org.antlr.v4.runtime.CharStreams; //導入依賴的package包/類
private static Program combineInput(boolean literate, String... fileNames) throws IOException {
final Program result = new Program();
for (String fileName : fileNames) {
CharStream stream;
if (!literate) {
stream = CharStreams.fromFileName(fileName);
} else {
stream = CharStreams.fromChannel(
streamToChannel(literate(lines(Paths.get(fileName)))),
4096,
CodingErrorAction.REPLACE,
fileName
);
}
result.accumulate(PARSER.parse(stream));
}
return result;
}
示例12: createRequest
import org.antlr.v4.runtime.CharStreams; //導入依賴的package包/類
public static FillerRequest createRequest(final SourceLinesProvider linesProvider, final InputFile file, final Charset encoding)
throws IOException, FileNotFoundException {
final CharStream mainStream = CharStreams.fromPath(file.path(), encoding);
final CharStream charStream = new CaseChangingCharStream(mainStream, true);
final TSqlLexer lexer = new TSqlLexer(charStream);
lexer.removeErrorListeners();
final CommonTokenStream stream = new CommonTokenStream(lexer);
stream.fill();
final TSqlParser parser = new TSqlParser(stream);
parser.removeErrorListeners();
final ParseTree root = parser.tsql_file();
final FillerRequest antrlFile = new FillerRequest(file, stream, root,
linesProvider.getLines(new FileInputStream(file.file()), encoding));
return antrlFile;
}
示例13: 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);
}
示例14: 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 + " \"");
}
}
}
示例15: 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;
}