org.antlr.v4.tool.LexerGrammar Java Examples

The following examples show how to use org.antlr.v4.tool.LexerGrammar. 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: ParsingUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static ParsingResult parseText(Grammar g,
									  LexerGrammar lg,
									  String startRuleName,
									  final VirtualFile grammarFile,
									  String inputText,
									  Project project) {
	ANTLRv4GrammarProperties grammarProperties = getGrammarProperties(project, grammarFile);
	CharStream input = grammarProperties.getCaseChangingStrategy()
			.applyTo(CharStreams.fromString(inputText, grammarFile.getPath()));
	LexerInterpreter lexEngine;
	lexEngine = lg.createLexerInterpreter(input);
	SyntaxErrorListener syntaxErrorListener = new SyntaxErrorListener();
	lexEngine.removeErrorListeners();
	lexEngine.addErrorListener(syntaxErrorListener);
	CommonTokenStream tokens = new TokenStreamSubset(lexEngine);
	return parseText(g, lg, startRuleName, grammarFile, syntaxErrorListener, tokens, 0);
}
 
Example #2
Source File: LexerATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public LexerATNFactory(LexerGrammar g) {
	super(g);
	// use codegen to get correct language templates for lexer commands
	String language = g.getOptionString("language");
	CodeGenerator gen = new CodeGenerator(g.tool, null, language);
	codegenTemplates = gen.getTemplates();
}
 
Example #3
Source File: LexerATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected Integer getConstantValue(String name, Token token) {
	if (name == null) {
		return null;
	}

	Integer commonConstant = COMMON_CONSTANTS.get(name);
	if (commonConstant != null) {
		return commonConstant;
	}

	int tokenType = g.getTokenType(name);
	if (tokenType != org.antlr.v4.runtime.Token.INVALID_TYPE) {
		return tokenType;
	}

	int channelValue = g.getChannelValue(name);
	if (channelValue >= org.antlr.v4.runtime.Token.MIN_USER_CHANNEL_VALUE) {
		return channelValue;
	}

	List<String> modeNames = new ArrayList<String>(((LexerGrammar)g).modes.keySet());
	int mode = modeNames.indexOf(name);
	if (mode >= 0) {
		return mode;
	}

	try {
		return Integer.parseInt(name);
	} catch (NumberFormatException ex) {
		g.tool.errMgr.grammarError(ErrorType.UNKNOWN_LEXER_CONSTANT, g.fileName, token, currentRule.name, token != null ? token.getText() : null);
		return null;
	}
}
 
Example #4
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ParserATNFactory(Grammar g) {
	if (g == null) {
		throw new NullPointerException("g");
	}

	this.g = g;

	ATNType atnType = g instanceof LexerGrammar ? ATNType.LEXER : ATNType.PARSER;
	int maxTokenType = g.getMaxTokenType();
	this.atn = new ATN(atnType, maxTokenType);
}
 
Example #5
Source File: Lexer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Lexer(OutputModelFactory factory, LexerFile file) {
	super(factory);
	this.file = file; // who contains us?

	Grammar g = factory.getGrammar();
	channels = new LinkedHashMap<String, Integer>(g.channelNameToValueMap);
	modes = ((LexerGrammar)g).modes.keySet();
}
 
Example #6
Source File: SemanticPipeline.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Assign constant values to custom channels defined in a grammar.
 *
 * @param g The grammar.
 * @param channelDefs A collection of AST nodes defining individual channels
 * within a {@code channels{}} block in the grammar.
 */
void assignChannelTypes(Grammar g, List<GrammarAST> channelDefs) {
	Grammar outermost = g.getOutermostGrammar();
	for (GrammarAST channel : channelDefs) {
		String channelName = channel.getText();

		// Channel names can't alias tokens or modes, because constant
		// values are also assigned to them and the ->channel(NAME) lexer
		// command does not distinguish between the various ways a constant
		// can be declared. This method does not verify that channels do not
		// alias rules, because rule names are not associated with constant
		// values in ANTLR grammar semantics.

		if (g.getTokenType(channelName) != Token.INVALID_TYPE) {
			g.tool.errMgr.grammarError(ErrorType.CHANNEL_CONFLICTS_WITH_TOKEN, g.fileName, channel.token, channelName);
		}

		if (LexerATNFactory.COMMON_CONSTANTS.containsKey(channelName)) {
			g.tool.errMgr.grammarError(ErrorType.CHANNEL_CONFLICTS_WITH_COMMON_CONSTANTS, g.fileName, channel.token, channelName);
		}

		if (outermost instanceof LexerGrammar) {
			LexerGrammar lexerGrammar = (LexerGrammar)outermost;
			if (lexerGrammar.modes.containsKey(channelName)) {
				g.tool.errMgr.grammarError(ErrorType.CHANNEL_CONFLICTS_WITH_MODE, g.fileName, channel.token, channelName);
			}
		}

		outermost.defineChannelName(channel.getText());
	}
}
 
Example #7
Source File: ANTLRv4PluginController.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String updateGrammarObjectsFromFile_(VirtualFile grammarFile) {
	String grammarFileName = grammarFile.getPath();
	PreviewState previewState = getPreviewState(grammarFile);
	Grammar[] grammars = ParsingUtils.loadGrammars(grammarFile, project);
	if (grammars != null) {
		synchronized (previewState) { // build atomically
			previewState.lg = (LexerGrammar)grammars[0];
			previewState.g = grammars[1];
		}
	}
	return grammarFileName;
}
 
Example #8
Source File: Tool.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void processNonCombinedGrammar(Grammar g, boolean gencode) {
	if ( g.ast==null || g.ast.hasErrors ) return;
	if ( internalOption_PrintGrammarTree ) System.out.println(g.ast.toStringTree());

	boolean ruleFail = checkForRuleIssues(g);
	if ( ruleFail ) return;

	int prevErrors = errMgr.getNumErrors();
	// MAKE SURE GRAMMAR IS SEMANTICALLY CORRECT (FILL IN GRAMMAR OBJECT)
	SemanticPipeline sem = new SemanticPipeline(g);
	sem.process();

	String language = g.getOptionString("language");
	if ( !CodeGenerator.targetExists(language) ) {
		errMgr.toolError(ErrorType.CANNOT_CREATE_TARGET_GENERATOR, language);
		return;
	}

	if ( errMgr.getNumErrors()>prevErrors ) return;

	// BUILD ATN FROM AST
	ATNFactory factory;
	if ( g.isLexer() ) factory = new LexerATNFactory((LexerGrammar)g);
	else factory = new ParserATNFactory(g);
	g.atn = factory.createATN();

	if ( generate_ATN_dot ) generateATNs(g);

	// PERFORM GRAMMAR ANALYSIS ON ATN: BUILD DECISION DFAs
	AnalysisPipeline anal = new AnalysisPipeline(g);
	anal.process();

	//if ( generate_DFA_dot ) generateDFAs(g);

	if ( g.tool.getNumErrors()>prevErrors ) return;

	// GENERATE CODE
	if ( gencode ) {
		CodeGenPipeline gen = new CodeGenPipeline(g);
		gen.process();
	}
}
 
Example #9
Source File: UseDefAnalyzer.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static Map<Rule, Set<Rule>> getRuleDependencies(LexerGrammar g, String modeName) {
	return getRuleDependencies(g, g.modes.get(modeName));
}
 
Example #10
Source File: ParsingUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static ParsingResult parseText(Grammar g,
										  LexerGrammar lg,
										  String startRuleName,
										  final VirtualFile grammarFile,
										  SyntaxErrorListener syntaxErrorListener,
										  TokenStream tokens,
										  int startIndex) {
		if ( g==null || lg==null ) {
			ANTLRv4PluginController.LOG.info("parseText can't parse: missing lexer or parser no Grammar object for " +
											 (grammarFile != null ? grammarFile.getName() : "<unknown file>"));
			return null;
		}

		String grammarFileName = g.fileName;
		if (!new File(grammarFileName).exists()) {
			ANTLRv4PluginController.LOG.info("parseText grammar doesn't exist "+grammarFileName);
			return null;
		}

		if ( g==BAD_PARSER_GRAMMAR || lg==BAD_LEXER_GRAMMAR ) {
			return null;
		}

		tokens.seek(startIndex);

		PreviewParser parser = new PreviewParser(g, tokens);
		parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
		parser.setProfile(true);

		parser.removeErrorListeners();
		parser.addErrorListener(syntaxErrorListener);

		Rule start = g.getRule(startRuleName);
		if ( start==null ) {
			return null; // can't find start rule
		}
//		System.out.println("parse test ----------------------------");
		ParseTree t = parser.parse(start.index);

		if ( t!=null ) {
			return new ParsingResult(parser, t, syntaxErrorListener);
		}
		return null;
	}