本文整理匯總了Java中org.antlr.v4.runtime.CharStreams.fromStream方法的典型用法代碼示例。如果您正苦於以下問題:Java CharStreams.fromStream方法的具體用法?Java CharStreams.fromStream怎麽用?Java CharStreams.fromStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.antlr.v4.runtime.CharStreams
的用法示例。
在下文中一共展示了CharStreams.fromStream方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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));
}
示例2: parseYangSource
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
private static StatementContext parseYangSource(final SourceIdentifier source, final InputStream stream)
throws IOException, YangSyntaxErrorException {
final YangStatementLexer lexer = new YangStatementLexer(CharStreams.fromStream(stream));
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final YangStatementParser parser = new YangStatementParser(tokens);
//disconnect from console error output
parser.removeErrorListeners();
final YangErrorListener errorListener = new YangErrorListener(source);
parser.addErrorListener(errorListener);
final StatementContext result = parser.statement();
errorListener.validate();
// Walk the resulting tree and replace each children with an immutable list, lowering memory requirements
// and making sure the resulting tree will not get accidentally modified. An alternative would be to use
// org.antlr.v4.runtime.Parser.TrimToSizeListener, but that does not make the tree immutable.
ParseTreeWalker.DEFAULT.walk(MAKE_IMMUTABLE_LISTENER, result);
return result;
}
示例3: parseStream
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
private static RulesetGroup parseStream(RulesetGroup rulesetGroup, InputStream is)
throws IOException {
CharStream input = CharStreams.fromStream( is );
NominalLexer nominalLexer = new NominalLexer( input );
CommonTokenStream tokens = new CommonTokenStream( nominalLexer );
NominalParser nominalParser = new NominalParser( tokens );
ParseTree parseTree = nominalParser.file();
NominalVisitor nominalVisitor =
new NominalVisitorImplementation( rulesetGroup );
nominalVisitor.visit( parseTree );
addDummyRulesets( rulesetGroup );
return rulesetGroup;
}
示例4: run
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
public static int run(final String data) throws IOException {
CharStream input;
if (data == null) {
// Create a CharStream that reads from standard input.
input = CharStreams.fromStream(System.in);
} else {
input = CharStreams.fromString(data);
}
// Create a lexer that feeds off of input CharStream.
final ExprAdvancedLexer lexer = new ExprAdvancedLexer(input);
// Create a buffer of tokens pulled from the lexer.
final CommonTokenStream tokens = new CommonTokenStream(lexer);
// Create a parser that feeds off the tokens buffer.
final ExprAdvancedParser parser = new ExprAdvancedParser(tokens);
// Begin parsing at "prog" rule.
final ParseTree tree = parser.prog();
final ExprVisitor eval = new ExprVisitor();
eval.visit(tree);
return eval.getResult();
}
示例5: getWorkspaceContext
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
public static WorkspaceContext getWorkspaceContext(InputStream source) throws IOException {
WorkspaceLexer l = new WorkspaceLexer(CharStreams.fromStream(source));
WorkspaceParser p = new WorkspaceParser(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("failed to parse at line "
+ line + " due to " + msg, e);
}
});
WorkspaceContext ctx = p.workspace();
return ctx;
}
示例6: testParse
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
@Test
public void testParse() throws Exception {
WorkspaceLexer l = new WorkspaceLexer(CharStreams.fromStream(getClass().getResourceAsStream("/thing.wsp")));
WorkspaceParser p = new WorkspaceParser(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("failed to parse at line "
+ line + " due to " + msg, e);
}
});
p.workspace();
}
示例7: manifest
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
public static Spa manifest(String resource) throws IOException {
SpaLexer l = new SpaLexer(CharStreams.fromStream(Utils.resolveResource(Spa.class,
resource)));
SpaParser p = new SpaParser(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("failed to parse at line "
+ line + " due to " + msg, e);
}
});
SpaContext spa = p.spa();
SpaImporter importer = new SpaImporter();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(importer, spa);
return importer.getSpa();
}
示例8: read
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
@Nullable
@Override
public CharStream read(String name) {
try {
InputStream resource = readResource(name);
if (resource != null) {
return CharStreams.fromStream(resource);
}
} catch (Exception e) {
LOGGER.error("Could not read {}", name, e);
}
return null;
}
示例9: parseLeafRefPathSource
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
private Path_argContext parseLeafRefPathSource(final InputStream stream) throws IOException,
LeafRefYangSyntaxErrorException {
final LeafRefPathLexer lexer = new LeafRefPathLexer(CharStreams.fromStream(stream));
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final LeafRefPathParser parser = new LeafRefPathParser(tokens);
parser.removeErrorListeners();
final LeafRefPathErrorListener errorListener = new LeafRefPathErrorListener(module);
parser.addErrorListener(errorListener);
final Path_argContext result = parser.path_arg();
errorListener.validate();
return result;
}
示例10: run
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
public static int run(final String data) throws IOException {
CharStream input;
if (data == null) {
// Create a CharStream that reads from standard input.
input = CharStreams.fromStream(System.in);
} else {
input = CharStreams.fromString(data);
}
// Create a lexer that feeds off of input CharStream.
final ExprAdvancedLexer lexer = new ExprAdvancedLexer(input);
// Create a buffer of tokens pulled from the lexer.
final CommonTokenStream tokens = new CommonTokenStream(lexer);
// Create a parser that feeds off the tokens buffer.
final ExprAdvancedParser parser = new ExprAdvancedParser(tokens);
// Begin parsing at "prog" rule.
final ParseTree tree = parser.prog();
// Create a generic parse tree walker that can trigger callbacks.
final ParseTreeWalker walker = new ParseTreeWalker();
// Walk the tree created during the parse, trigger callbacks.
final ExprListener listener = new ExprListener();
walker.walk(listener, tree);
return listener.getResult();
}
示例11: doParse
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
protected void doParse(final File inputFile, final VbParserParams params) throws IOException {
final Charset charset = params.getCharset();
LOG.info("Parsing file {} with charset {}.", inputFile.getName(), charset);
final InputStream inputStream = new FileInputStream(inputFile);
final VisualBasic6Lexer lexer = new VisualBasic6Lexer(CharStreams.fromStream(inputStream, charset));
if (!params.getIgnoreSyntaxErrors()) {
lexer.removeErrorListeners();
lexer.addErrorListener(new ThrowingErrorListener());
}
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final VisualBasic6Parser parser = new VisualBasic6Parser(tokens);
if (!params.getIgnoreSyntaxErrors()) {
parser.removeErrorListeners();
parser.addErrorListener(new ThrowingErrorListener());
}
final StartRuleContext startRule = parser.startRule();
final File treeFile = new File(inputFile.getAbsolutePath() + TREE_SUFFIX);
if (treeFile.exists()) {
doCompareParseTree(treeFile, startRule, parser);
}
}
示例12: compile
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
/**
* Compile it to Java and save.
* @throws IOException If fails
*/
public void compile() throws IOException {
final String[] lines = new TextOf(this.input).asString().split("\n");
final ANTLRErrorListener errors = new BaseErrorListener() {
// @checkstyle ParameterNumberCheck (10 lines)
@Override
public void syntaxError(final Recognizer<?, ?> recognizer,
final Object symbol, final int line,
final int position, final String msg,
final RecognitionException error) {
throw new CompileException(
String.format(
"[%d:%d] %s: \"%s\"",
line, position, msg, lines[line - 1]
),
error
);
}
};
final ProgramLexer lexer = new ProgramLexer(
CharStreams.fromStream(this.input.stream())
);
lexer.removeErrorListeners();
lexer.addErrorListener(errors);
final ProgramParser parser = new ProgramParser(
new CommonTokenStream(lexer)
);
parser.removeErrorListeners();
parser.addErrorListener(errors);
final Tree tree = parser.program().ret;
new IoCheckedScalar<>(
new And(
tree.java().entrySet(),
path -> {
new LengthOf(
new TeeInput(
path.getValue(),
this.target.apply(path.getKey())
)
).value();
}
)
).value();
}
示例13: getFull
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
public static AntrlResult getFull(InputStream stream) throws IOException {
final CharStream charStream = CharStreams.fromStream(stream);
return getFromStream(charStream);
}
示例14: parseRegexList
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
public static RegexList parseRegexList(URL resourceURL, Optional<Path> customResourceDir) throws IOException {
InputStream is = resourceURL.openStream();
CharStream input = CharStreams.fromStream(is);
return parseRegexList(input, resourceURL, customResourceDir);
}
示例15: parseFile
import org.antlr.v4.runtime.CharStreams; //導入方法依賴的package包/類
protected void parseFile(final File inputFile, final Program program, final VbParserParams params)
throws IOException {
if (!inputFile.isFile()) {
LOG.warn("Could not find file {}", inputFile.getAbsolutePath());
} else {
final Charset charset = params.getCharset();
LOG.info("Parsing file {} with charset {}.", inputFile.getName(), charset);
final InputStream inputStream = new FileInputStream(inputFile);
final VisualBasic6Lexer lexer = new VisualBasic6Lexer(CharStreams.fromStream(inputStream, charset));
if (!params.getIgnoreSyntaxErrors()) {
// register an error listener, so that preprocessing stops on errors
lexer.removeErrorListeners();
lexer.addErrorListener(new ThrowingErrorListener());
}
// get a list of matched tokens
final CommonTokenStream tokens = new CommonTokenStream(lexer);
// pass the tokens to the parser
final VisualBasic6Parser parser = new VisualBasic6Parser(tokens);
if (!params.getIgnoreSyntaxErrors()) {
// register an error listener, so that preprocessing stops on errors
parser.removeErrorListeners();
parser.addErrorListener(new ThrowingErrorListener());
}
// specify our entry point
final StartRuleContext ctx = parser.startRule();
// determine the module name
final String declaredModuleName = analyzeDeclaredModuleName(ctx);
final String moduleName;
if (declaredModuleName != null && !declaredModuleName.isEmpty()) {
moduleName = declaredModuleName;
} else {
moduleName = getModuleName(inputFile);
}
// analyze contained modules and types
final boolean isClazzModule = isClazzModule(inputFile);
final boolean isStandardModule = isStandardModule(inputFile);
final String input = FileUtils.readFileToString(inputFile, charset);
final List<String> lines = splitLines(input);
final ParserVisitor visitor = new VbModuleVisitorImpl(moduleName, lines, isClazzModule, isStandardModule,
tokens, program);
LOG.info("Collecting types in file {}.", inputFile.getName());
visitor.visit(ctx);
}
}