• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java Grammar类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.antlr.v4.tool.Grammar的典型用法代码示例。如果您正苦于以下问题:Java Grammar类的具体用法?Java Grammar怎么用?Java Grammar使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



Grammar类属于org.antlr.v4.tool包,在下文中一共展示了Grammar类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: handleOptionSetArg

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
protected void handleOptionSetArg(String arg) {
	int eq = arg.indexOf('=');
	if ( eq>0 && arg.length()>3 ) {
		String option = arg.substring("-D".length(), eq);
		String value = arg.substring(eq+1);
		if ( value.length()==0 ) {
			errMgr.toolError(ErrorType.BAD_OPTION_SET_SYNTAX, arg);
			return;
		}
		if ( Grammar.parserOptions.contains(option) ||
			 Grammar.lexerOptions.contains(option) )
		{
			if ( grammarOptions==null ) grammarOptions = new HashMap<String, String>();
			grammarOptions.put(option, value);
		}
		else {
			errMgr.grammarError(ErrorType.ILLEGAL_OPTION,
								null,
								null,
								option);
		}
	}
	else {
		errMgr.toolError(ErrorType.BAD_OPTION_SET_SYNTAX, arg);
	}
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:27,代码来源:Tool.java


示例2: processGrammarsOnCommandLine

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public void processGrammarsOnCommandLine() {
	List<GrammarRootAST> sortedGrammars = sortGrammarByTokenVocab(grammarFiles);

	for (GrammarRootAST t : sortedGrammars) {
		final Grammar g = createGrammar(t);
		g.fileName = t.fileName;
		if ( gen_dependencies ) {
			BuildDependencyGenerator dep =
				new BuildDependencyGenerator(this, g);
			/*
				List outputFiles = dep.getGeneratedFileList();
				List dependents = dep.getDependenciesFileList();
				System.out.println("output: "+outputFiles);
				System.out.println("dependents: "+dependents);
				 */
			System.out.println(dep.getDependencies().render());

		}
		else if (errMgr.getNumErrors() == 0) {
			process(g, true);
		}
	}
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:24,代码来源:Tool.java


示例3: generateATNs

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public void generateATNs(Grammar g) {
	DOTGenerator dotGenerator = new DOTGenerator(g);
	List<Grammar> grammars = new ArrayList<Grammar>();
	grammars.add(g);
	List<Grammar> imported = g.getAllImportedGrammars();
	if ( imported!=null ) grammars.addAll(imported);
	for (Grammar ig : grammars) {
		for (Rule r : ig.rules.values()) {
			try {
				String dot = dotGenerator.getDOT(g.atn.ruleToStartState[r.index], g.isLexer());
				if (dot != null) {
					writeDOTFile(g, r, dot);
				}
			}
               catch (IOException ioe) {
				errMgr.toolError(ErrorType.CANNOT_WRITE_FILE, ioe);
			}
		}
	}
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:21,代码来源:Tool.java


示例4: getOutputFileWriter

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
/** This method is used by all code generators to create new output
 *  files. If the outputDir set by -o is not present it will be created.
 *  The final filename is sensitive to the output directory and
 *  the directory where the grammar file was found.  If -o is /tmp
 *  and the original grammar file was foo/t.g4 then output files
 *  go in /tmp/foo.
 *
 *  The output dir -o spec takes precedence if it's absolute.
 *  E.g., if the grammar file dir is absolute the output dir is given
 *  precendence. "-o /tmp /usr/lib/t.g4" results in "/tmp/T.java" as
 *  output (assuming t.g4 holds T.java).
 *
 *  If no -o is specified, then just write to the directory where the
 *  grammar file was found.
 *
 *  If outputDirectory==null then write a String.
 */
public Writer getOutputFileWriter(Grammar g, String fileName) throws IOException {
	if (outputDirectory == null) {
		return new StringWriter();
	}
	// output directory is a function of where the grammar file lives
	// for subdir/T.g4, you get subdir here.  Well, depends on -o etc...
	File outputDir = getOutputDirectory(g.fileName);
	File outputFile = new File(outputDir, fileName);

	if (!outputDir.exists()) {
		outputDir.mkdirs();
	}
	FileOutputStream fos = new FileOutputStream(outputFile);
	OutputStreamWriter osw;
	if ( grammarEncoding!=null ) {
		osw = new OutputStreamWriter(fos, grammarEncoding);
	}
	else {
		osw = new OutputStreamWriter(fos);
	}
	return new BufferedWriter(osw);
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:40,代码来源:Tool.java


示例5: ParserFile

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public ParserFile(OutputModelFactory factory, String fileName) {
	super(factory, fileName);
	Grammar g = factory.getGrammar();
	namedActions = new HashMap<String, Action>();
	for (String name : g.namedActions.keySet()) {
		ActionAST ast = g.namedActions.get(name);
		namedActions.put(name, new Action(factory, ast));
	}
	genPackage = g.tool.genPackage;
	// need the below members in the ST for Python
	genListener = g.tool.gen_listener;
	genVisitor = g.tool.gen_visitor;
	grammarName = g.name;

	if (g.getOptionString("contextSuperClass") != null) {
		contextSuperClass = new ActionText(null, g.getOptionString("contextSuperClass"));
	}
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:19,代码来源:ParserFile.java


示例6: ListenerFile

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public ListenerFile(OutputModelFactory factory, String fileName) {
	super(factory, fileName);
	Grammar g = factory.getGrammar();
	parserName = g.getRecognizerName();
	grammarName = g.name;
	for (Rule r : g.rules.values()) {
		Map<String, List<Pair<Integer,AltAST>>> labels = r.getAltLabels();
		if ( labels!=null ) {
			for (Map.Entry<String, List<Pair<Integer, AltAST>>> pair : labels.entrySet()) {
				listenerNames.add(pair.getKey());
				listenerLabelRuleNames.put(pair.getKey(), r.name);
			}
		}
		else {
			// only add rule context if no labels
			listenerNames.add(r.name);
		}
	}
	ActionAST ast = g.namedActions.get("header");
	if ( ast!=null ) header = new Action(factory, ast);
	genPackage = factory.getGrammar().tool.genPackage;
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:23,代码来源:ListenerFile.java


示例7: VisitorFile

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public VisitorFile(OutputModelFactory factory, String fileName) {
	super(factory, fileName);
	Grammar g = factory.getGrammar();
	parserName = g.getRecognizerName();
	grammarName = g.name;
	for (Rule r : g.rules.values()) {
		Map<String, List<Pair<Integer, AltAST>>> labels = r.getAltLabels();
		if ( labels!=null ) {
			for (Map.Entry<String, List<Pair<Integer, AltAST>>> pair : labels.entrySet()) {
				visitorNames.add(pair.getKey());
				visitorLabelRuleNames.put(pair.getKey(), r.name);
			}
		}
		else {
			// if labels, must label all. no need for generic rule visitor then
			visitorNames.add(r.name);
		}
	}
	ActionAST ast = g.namedActions.get("header");
	if ( ast!=null ) header = new Action(factory, ast);
	genPackage = factory.getGrammar().tool.genPackage;
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:23,代码来源:VisitorFile.java


示例8: getRuleDependencies

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public static Map<Rule, Set<Rule>> getRuleDependencies(Grammar g, Collection<Rule> rules) {
	Map<Rule, Set<Rule>> dependencies = new HashMap<Rule, Set<Rule>>();

	for (Rule r : rules) {
		List<GrammarAST> tokenRefs = r.ast.getNodesWithType(ANTLRParser.TOKEN_REF);
		for (GrammarAST tref : tokenRefs) {
			Set<Rule> calls = dependencies.get(r);
			if ( calls==null ) {
				calls = new HashSet<Rule>();
				dependencies.put(r, calls);
			}
			calls.add(g.getRule(tref.getText()));
		}
	}

	return dependencies;
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:18,代码来源:UseDefAnalyzer.java


示例9: checkGrammarName

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
void checkGrammarName(Token nameToken) {
	String fullyQualifiedName = nameToken.getInputStream().getSourceName();
	if (fullyQualifiedName == null) {
		// This wasn't read from a file.
		return;
	}

	File f = new File(fullyQualifiedName);
	String fileName = f.getName();
	if ( g.originalGrammar!=null ) return; // don't warn about diff if this is implicit lexer
	if ( !Utils.stripFileExtension(fileName).equals(nameToken.getText()) &&
	     !fileName.equals(Grammar.GRAMMAR_FROM_STRING_NAME)) {
		g.tool.errMgr.grammarError(ErrorType.FILE_AND_GRAMMAR_NAME_DIFFER,
								   fileName, nameToken, nameToken.getText(), fileName);
	}
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:17,代码来源:BasicSemanticChecks.java


示例10: checkInvalidRuleDef

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
void checkInvalidRuleDef(Token ruleID) {
	String fileName = null;
	if ( ruleID.getInputStream()!=null ) {
		fileName = ruleID.getInputStream().getSourceName();
	}
	if ( g.isLexer() && Character.isLowerCase(ruleID.getText().charAt(0)) ) {
		g.tool.errMgr.grammarError(ErrorType.PARSER_RULES_NOT_ALLOWED,
								   fileName, ruleID, ruleID.getText());
	}
	if ( g.isParser() &&
		Grammar.isTokenName(ruleID.getText()) )
	{
		g.tool.errMgr.grammarError(ErrorType.LEXER_RULES_NOT_ALLOWED,
								   fileName, ruleID, ruleID.getText());
	}
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:17,代码来源:BasicSemanticChecks.java


示例11: checkImport

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
void checkImport(Token importID) {
	Grammar delegate = g.getImportedGrammar(importID.getText());
	if ( delegate==null ) return;
	List<Integer> validDelegators = validImportTypes.get(delegate.getType());
	if ( validDelegators!=null && !validDelegators.contains(g.getType()) ) {
		g.tool.errMgr.grammarError(ErrorType.INVALID_IMPORT,
								   g.fileName,
								   importID,
								   g, delegate);
	}
	if ( g.isCombined() &&
		 (delegate.name.equals(g.name+Grammar.getGrammarTypeToFileNameSuffix(ANTLRParser.LEXER))||
		  delegate.name.equals(g.name+Grammar.getGrammarTypeToFileNameSuffix(ANTLRParser.PARSER))) )
	{
		g.tool.errMgr.grammarError(ErrorType.IMPORT_NAME_CLASH,
								   g.fileName,
								   importID,
								   g, delegate);
	}
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:21,代码来源:BasicSemanticChecks.java


示例12: SymbolChecks

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public SymbolChecks(Grammar g, SymbolCollector collector) {
      this.g = g;
      this.collector = collector;
this.errMgr = g.tool.errMgr;

      for (GrammarAST tokenId : collector.tokenIDRefs) {
          tokenIDs.add(tokenId.getText());
      }
      /*
      System.out.println("rules="+collector.rules);
      System.out.println("rulerefs="+collector.rulerefs);
      System.out.println("tokenIDRefs="+collector.tokenIDRefs);
      System.out.println("terminals="+collector.terminals);
      System.out.println("strings="+collector.strings);
      System.out.println("tokensDef="+collector.tokensDefs);
      System.out.println("actions="+collector.actions);
      System.out.println("scopes="+collector.scopes);
       */
  }
 
开发者ID:antlr,项目名称:codebuff,代码行数:20,代码来源:SymbolChecks.java


示例13: checkRuleArgs

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public void checkRuleArgs(Grammar g, List<GrammarAST> rulerefs) {
	if ( rulerefs==null ) return;
	for (GrammarAST ref : rulerefs) {
		String ruleName = ref.getText();
		Rule r = g.getRule(ruleName);
		GrammarAST arg = (GrammarAST)ref.getFirstChildWithType(ANTLRParser.ARG_ACTION);
		if ( arg!=null && (r==null || r.args==null) ) {
			errMgr.grammarError(ErrorType.RULE_HAS_NO_ARGS,
									  g.fileName, ref.token, ruleName);

		}
		else if ( arg==null && (r!=null&&r.args!=null) ) {
			errMgr.grammarError(ErrorType.MISSING_RULE_ARGS,
									  g.fileName, ref.token, ruleName);
		}
	}
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:18,代码来源:SymbolChecks.java


示例14: checkForQualifiedRuleIssues

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public void checkForQualifiedRuleIssues(Grammar g, List<GrammarAST> qualifiedRuleRefs) {
	for (GrammarAST dot : qualifiedRuleRefs) {
		GrammarAST grammar = (GrammarAST)dot.getChild(0);
		GrammarAST rule = (GrammarAST)dot.getChild(1);
           g.tool.log("semantics", grammar.getText()+"."+rule.getText());
		Grammar delegate = g.getImportedGrammar(grammar.getText());
		if ( delegate==null ) {
			errMgr.grammarError(ErrorType.NO_SUCH_GRAMMAR_SCOPE,
									  g.fileName, grammar.token, grammar.getText(),
									  rule.getText());
		}
		else {
			if ( g.getRule(grammar.getText(), rule.getText())==null ) {
				errMgr.grammarError(ErrorType.NO_SUCH_RULE_IN_SCOPE,
										  g.fileName, rule.token, grammar.getText(),
										  rule.getText());
			}
		}
	}
}
 
开发者ID:antlr,项目名称:codebuff,代码行数:21,代码来源:SymbolChecks.java


示例15: isolatedRuleRef

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public Rule isolatedRuleRef(String x) {
if ( node.resolver instanceof Grammar ) return null;

if ( x.equals(r.name) ) return r;
List<LabelElementPair> labels = null;
if ( node.resolver instanceof Rule ) {
	labels = r.getElementLabelDefs().get(x);
}
else if ( node.resolver instanceof Alternative ) {
	labels = ((Alternative)node.resolver).labelDefs.get(x);
}
if ( labels!=null ) {  // it's a label ref. is it a rule label?
	LabelElementPair anyLabelDef = labels.get(0);
	if ( anyLabelDef.type==LabelType.RULE_LABEL ) {
		return g.getRule(anyLabelDef.element.getText());
	}
}
if ( node.resolver instanceof Alternative ) {
	if ( ((Alternative)node.resolver).ruleRefs.get(x)!=null ) {
		return g.getRule(x);
	}
}
      return null;
  }
 
开发者ID:antlr,项目名称:codebuff,代码行数:25,代码来源:AttributeChecks.java


示例16: computeHighlighting

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
private static StyleSpans<Collection<String>> computeHighlighting(Grammar g, Map<String, String> styles, String text){
    System.out.println("Starting to compute highlighting");
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();

    if(text.length() > 0){
        LexerInterpreter lex = g.createLexerInterpreter(new ANTLRInputStream(text));
        CommonTokenStream tokenStream = new CommonTokenStream(lex);

        // parse
        int lastEnd = 0;
        for(Token t: lex.getAllTokens()){
            int spacer = t.getStartIndex() - lastEnd;
            if(spacer > 0) {
                spansBuilder.add(Collections.emptyList(), spacer);

                int gap = t.getText().length();
                spansBuilder.add(Collections.singleton(getStyleClass(lex,styles,t)), gap);
                lastEnd = t.getStopIndex() + 1;
            }
        }
    }else{
        spansBuilder.add(Collections.emptyList(), 0);
    }
    return spansBuilder.create();
}
 
开发者ID:jeffreyguenther,项目名称:Xanthic,代码行数:26,代码来源:SyntaxArea.java


示例17: CompiledFileModelV4

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public CompiledFileModelV4(@NullAllowed Grammar grammar, @NullAllowed GrammarRootAST result, @NonNull List<? extends SyntaxError> syntaxErrors, @NonNull FileObject fileObject, @NullAllowed CommonToken[] tokens) {
    super(fileObject, tokens);
    Parameters.notNull("syntaxErrors", syntaxErrors);

    this.grammar = grammar;
    this.result = result;
    this.syntaxErrors = syntaxErrors;
    this.importedResults = new ArrayList<>();
    if (grammar != null && grammar.importedGrammars != null) {
        for (Grammar imported : grammar.importedGrammars) {
            CommonTokenStream importedTokenStream = (CommonTokenStream)imported.ast.tokenStream;
            String fileName = ((Lexer)importedTokenStream.getTokenSource()).getCharStream().getSourceName();
            FileObject importedFileObject = FileUtil.toFileObject(new File(fileName));
            List<? extends Token> importedTokensList = importedTokenStream.getTokens();
            CommonToken[] importedTokens = importedTokensList.toArray(new CommonToken[0]);
            importedResults.add(new CompiledFileModelV4(imported, imported.ast, Collections.<SyntaxError>emptyList(), importedFileObject, importedTokens));
        }
    }
}
 
开发者ID:tunnelvisionlabs,项目名称:goworks,代码行数:20,代码来源:CompiledFileModelV4.java


示例18: emit

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
@Override
public Token emit() {
    if (_type == ID) {
        String firstChar = _input.getText(Interval.of(_tokenStartCharIndex, _tokenStartCharIndex));
        if (Grammar.isTokenName(firstChar)) {
            _type = TOKEN_REF;
        } else {
            _type = RULE_REF;
        }

        if (_ruleType == Token.INVALID_TYPE) {
            _ruleType = _type;
        }
    } else if (_type == SEMI) {
        _ruleType = Token.INVALID_TYPE;
    }

    return super.emit();
}
 
开发者ID:tunnelvisionlabs,项目名称:goworks,代码行数:20,代码来源:GrammarLexer.java


示例19: visitTerminal

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
@Override
public void visitTerminal(TerminalNode node) {
    Token token = node.getSymbol();
    if (_grammarAnnotatedParseTree.getTokenDecorator().getProperty(token, GrammarTreeProperties.PROP_MISSING_DEF)) {
        String text = token.getText();
        if ("EOF".equals(text)) {
            return;
        }

        Grammar grammar = _compiledFileModel.getGrammar();
        if (grammar != null && grammar.rules.containsKey(text)) {
            return;
        }

        int startIndex = token.getStartIndex();
        int stopIndex = token.getStopIndex();
        _rewriteRanges.add(startIndex, stopIndex);
    }
}
 
开发者ID:tunnelvisionlabs,项目名称:goworks,代码行数:20,代码来源:MissingRuleDefinitionHintParserTask.java


示例20: getSemanticContextDisplayString

import org.antlr.v4.tool.Grammar; //导入依赖的package包/类
public static String getSemanticContextDisplayString(PredicateEvalInfo pred,
                                                     PreviewState previewState,
                                                     SemanticContext semctx,
                                                     int alt,
                                                     boolean result) {
	Grammar g = previewState.g;
	String semanticContextDisplayString = g.getSemanticContextDisplayString(semctx);
	if ( semctx instanceof SemanticContext.PrecedencePredicate ) {
		int ruleIndex = previewState.parsingResult.parser.getATN().decisionToState.get(pred.decision).ruleIndex;
		Rule rule = g.getRule(ruleIndex);
		int precedence = ((SemanticContext.PrecedencePredicate) semctx).precedence;
		// precedence = n - originalAlt + 1, So:
		int originalAlt = rule.getOriginalNumberOfAlts()-precedence+1;
		alt = originalAlt;
	}
	return semanticContextDisplayString+" => alt "+alt+" is "+result;
}
 
开发者ID:antlr,项目名称:intellij-plugin-v4,代码行数:18,代码来源:ProfilerPanel.java



注:本文中的org.antlr.v4.tool.Grammar类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java FileFormat类代码示例发布时间:2022-05-22
下一篇:
Java AfterSuite类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap