org.antlr.v4.Tool Java Examples

The following examples show how to use org.antlr.v4.Tool. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Grammar.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Grammar(Tool tool, GrammarRootAST ast) {
if ( ast==null ) {
	throw new NullPointerException("ast");
}

if (ast.tokenStream == null) {
	throw new IllegalArgumentException("ast must have a token stream");
}

      this.tool = tool;
      this.ast = ast;
      this.name = (ast.getChild(0)).getText();
this.tokenStream = ast.tokenStream;
this.originalTokenStream = this.tokenStream;

initTokenSymbolTables();
  }
 
Example #2
Source File: ConfigureANTLRAction.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
	if ( e.getProject()==null ) {
		LOG.error("actionPerformed no project for "+e);
		return; // whoa!
	}
	VirtualFile grammarFile = MyActionUtils.getGrammarFileFromEvent(e);
	if ( grammarFile==null ) return;
	LOG.info("actionPerformed "+grammarFile);

	ConfigANTLRPerGrammar configDialog = ConfigANTLRPerGrammar.getDialogForm(e.getProject(), grammarFile.getPath());
	configDialog.getPeer().setTitle("Configure ANTLR Tool "+ Tool.VERSION+" for "+ grammarFile.getName());

	configDialog.show();

	if ( configDialog.getExitCode()==DialogWrapper.OK_EXIT_CODE ) {
		configDialog.saveValues(e.getProject(), grammarFile.getPath());
	}
}
 
Example #3
Source File: ParsingUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static GrammarRootAST parseGrammar(Project project, Tool antlr, VirtualFile grammarFile) {
	try {
		Document document = FileDocumentManager.getInstance().getDocument(grammarFile);
		String grammarText = document != null ? document.getText() : new String(grammarFile.contentsToByteArray());

		ANTLRStringStream in = new ANTLRStringStream(grammarText);
		in.name = grammarFile.getPath();
		return antlr.parse(grammarFile.getPath(), in);
	}
	catch (IOException ioe) {
		antlr.errMgr.toolError(ErrorType.CANNOT_OPEN_FILE, ioe, grammarFile);
	}
	return null;
}
 
Example #4
Source File: ParsingUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
private static Grammar loadGrammar(VirtualFile grammarFile, Project project, Tool antlr) {
	// basically here I am mimicking the loadGrammar() method from Tool
	// so that I can check for an empty AST coming back.
	GrammarRootAST grammarRootAST = parseGrammar(project, antlr, grammarFile);
	if ( grammarRootAST==null ) {
		return null;
	}

	// Create a grammar from the AST so we can figure out what type it is
	Grammar g = antlr.createGrammar(grammarRootAST);
	g.fileName = grammarFile.getPath();

	return g;
}
 
Example #5
Source File: ParsingUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Tool createANTLRToolForLoadingGrammars() {
	Tool antlr = new Tool();
	antlr.errMgr = new PluginIgnoreMissingTokensFileErrorManager(antlr);
	antlr.errMgr.setFormat("antlr");
	LoadGrammarsToolListener listener = new LoadGrammarsToolListener(antlr);
	antlr.removeListeners();
	antlr.addListener(listener);
	return antlr;
}
 
Example #6
Source File: LeftRecursiveRuleAnalyzer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public LeftRecursiveRuleAnalyzer(GrammarAST ruleAST,
								 Tool tool, String ruleName, String language)
{
	super(new CommonTreeNodeStream(new GrammarASTAdaptor(ruleAST.token.getInputStream()), ruleAST));
	this.tool = tool;
	this.ruleName = ruleName;
	this.language = language;
	this.tokenStream = ruleAST.g.tokenStream;
	if (this.tokenStream == null) {
		throw new NullPointerException("grammar must have a token stream");
	}

	loadPrecRuleTemplates();
}
 
Example #7
Source File: Target.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public STGroup getTemplates() {
	if (templates == null) {
		String version = getVersion();
		if ( version==null ||
			 !RuntimeMetaData.getMajorMinorVersion(version).equals(RuntimeMetaData.getMajorMinorVersion(Tool.VERSION)))
		{
			gen.tool.errMgr.toolError(ErrorType.INCOMPATIBLE_TOOL_AND_TEMPLATES, version, Tool.VERSION, language);
		}
		templates = loadTemplates();
	}

	return templates;
}
 
Example #8
Source File: OutputFile.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public OutputFile(OutputModelFactory factory, String fileName) {
      super(factory);
      this.fileName = fileName;
      Grammar g = factory.getGrammar();
grammarFileName = g.fileName;
ANTLRVersion = Tool.VERSION;
      TokenLabelType = g.getOptionString("TokenLabelType");
      InputSymbolType = TokenLabelType;
  }
 
Example #9
Source File: OutputModelWalker.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public OutputModelWalker(Tool tool,
						 STGroup templates)
{
	this.tool = tool;
	this.templates = templates;
}
 
Example #10
Source File: RunANTLRListener.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public RunANTLRListener(Tool tool, ConsoleView console) {
	this.tool = tool;
	this.console = console;
}
 
Example #11
Source File: PluginIgnoreMissingTokensFileErrorManager.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public PluginIgnoreMissingTokensFileErrorManager(Tool tool) {
	super(tool);
}
 
Example #12
Source File: Antlr4ToolFacade.java    From openCypher with Apache License 2.0 4 votes vote down vote up
private Antlr4ToolFacade( Tool tool, Output.Readable buffer )
{
    this.tool = tool;
    this.buffer = buffer;
}
 
Example #13
Source File: Antlr4ToolFacade.java    From openCypher with Apache License 2.0 4 votes vote down vote up
public static void assertGeneratesValidParser( String resource ) throws Exception
{
    Output.Readable buffer = stringBuilder();
    Tool antlr = new Tool();
    Antlr4ToolFacade facade = new Antlr4ToolFacade( antlr, buffer );
    try
    {
        Antlr4.write( Fixture.grammarResource( Antlr4.class, resource ), buffer );
    }
    catch ( Throwable e )
    {
        try
        {
            facade.reportFailureIn( "generating grammar" );
        }
        catch ( AssertionError x )
        {
            throw e;
        }
    }
    antlr.addListener( facade );
    GrammarRootAST ast = antlr.parse( resource, new ANTLRReaderStream( buffer.reader() ) );
    if ( ast.hasErrors )
    {
        RuleAST lastGood = lastGoodRule( ast );
        if ( lastGood == null )
        {
            facade.reportFailureIn( "parsing grammar" );
        }
        else
        {
            facade.reportFailureIn(
                    "parsing grammar, after " + lastGood.getRuleName() + " on line " + lastGood.getLine() );
        }
    }
    antlr.process( antlr.createGrammar( ast ), false );
    if ( facade.hasErrors() )
    {
        facade.reportFailureIn( "processing grammar" );
    }
}
 
Example #14
Source File: CodeGenerator.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public CodeGenerator(Tool tool, Grammar g, String language) {
	this.g = g;
	this.tool = tool;
	this.language = language != null ? language : DEFAULT_LANGUAGE;
}
 
Example #15
Source File: BuildDependencyGenerator.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public BuildDependencyGenerator(Tool tool, Grammar g) {
      this.tool = tool;
this.g = g;
String language = g.getOptionString("language");
generator = new CodeGenerator(tool, g, language);
  }
 
Example #16
Source File: ToolANTLRParser.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ToolANTLRParser(TokenStream input, Tool tool) {
	super(input);
	this.tool = tool;
}
 
Example #17
Source File: ToolANTLRLexer.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ToolANTLRLexer(CharStream input, Tool tool) {
	super(input);
	this.tool = tool;
}
 
Example #18
Source File: Grammar.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/** convenience method for Tool.loadGrammar() */
public static Grammar load(String fileName) {
	Tool antlr = new Tool();
	return antlr.loadGrammar(fileName);
}
 
Example #19
Source File: LexerGrammar.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public LexerGrammar(Tool tool, GrammarRootAST ast) {
	super(tool, ast);
}
 
Example #20
Source File: ErrorManager.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ErrorManager(Tool tool) {
	this.tool = tool;
}
 
Example #21
Source File: GrammarTransformPipeline.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public GrammarTransformPipeline(Grammar g, Tool tool) {
	this.g = g;
	this.tool = tool;
}
 
Example #22
Source File: ParserGenerator.java    From ethereumj with MIT License 3 votes vote down vote up
public static void main(String args[]) throws MojoFailureException, MojoExecutionException {

        String userDir = System.getProperty("user.dir");

        String grammarName = userDir + "\\src\\main\\antlr4\\org\\ethereum\\serpent\\Serpent.g4";

        String options[] = {grammarName, "-visitor",  "-package", "org.ethereum.serpent"};
        Tool tool = new Tool(options);
        tool.outputDirectory = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\";
        tool.processGrammarsOnCommandLine();

//        org.antlr.Tool.main(new String[]{userDir + "\\src\\main\\antlr4\\org\\ethereum\\serpent\\Serpent.g4"});
    }
 
Example #23
Source File: LoadGrammarsToolListener.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License votes vote down vote up
public LoadGrammarsToolListener(Tool tool) { super(tool); } 
Example #24
Source File: DefaultToolListener.java    From codebuff with BSD 2-Clause "Simplified" License votes vote down vote up
public DefaultToolListener(Tool tool) { this.tool = tool; }