/** * Get the source of the string to parse. This method is undefined if it is called before calling * {@link #stringToParse()}. * @return A textual description of the source of the string to parse. */ public Source source() { if (expression != null) { return Source.apply("<command line: -e>"); } else { return Source.apply(files.get().resolveWorkingDirectory(parameters.get(0)).getAbsolutePath()); } }
/** * Parses the text to create a {@link Definition} object. * The main module of the definition will be last module defined in the text file. */ public static org.kframework.definition.Definition from(String definitionText) { Pattern pattern = Pattern.compile("(?:^|\\s)module ([A-Z][A-Z\\-]*)"); Matcher m = pattern.matcher(definitionText); if(!m.find()) { throw new RuntimeException("Could not find any module in the definition"); } String nameOfLastModule = m.group(m.groupCount()); return from(definitionText, nameOfLastModule, Source.apply("generated")); }
/** * Parses the text to create a {@link Definition} object. */ public static org.kframework.definition.Definition from(String definitionText, String mainModuleName) { return from(definitionText, mainModuleName, Source.apply("generated")); }
public static K parseWithFile(String theTextToParse, String mainModule, Sort startSymbol, File definitionFile) { String definitionText; try { definitionText = FileUtils.readFileToString(definitionFile); } catch (IOException e) { throw new RuntimeException(e); } return parseWithString(theTextToParse, mainModule, startSymbol, Source.apply(definitionFile.getAbsolutePath()), definitionText); }
/** * Parses a string with a particular start symbol/sort. * * @param startSymbol the start symbol/sort * @param toParse the String to parse * @return a pair: the left projection is a parsed string as a K, if successful; * the right projection is the set of issues encountered while parsing */ public Tuple2<Option<K>, Set<Warning>> apply(Sort startSymbol, String toParse) { return apply(startSymbol, toParse, Source.apply("generated")); }
public Parser(String input, Scanner scanner) { s = new ParseState(input, scanner, Source.apply("<unknown>"), 1, 1); }
private void addOpaqueKLabels(Set<KLabel> klabels) { if (options.klabels == null) return; File definitionFile = files.resolveWorkingDirectory(options.klabels).getAbsoluteFile(); List<File> lookupDirectories = kompileOptions.outerParsing.includes.stream().map(files::resolveWorkingDirectory).collect(Collectors.toList()); lookupDirectories.add(Kompile.BUILTIN_DIRECTORY); java.util.Set<Module> mods = new ParserUtils(files::resolveWorkingDirectory, kem, globalOptions).loadModules( new HashSet<>(), "require " + StringUtil.enquoteCString(definitionFile.getPath()), Source.apply(definitionFile.getAbsolutePath()), definitionFile.getParentFile(), lookupDirectories, new HashSet<>(), false); mods.stream().forEach(m -> klabels.addAll(mutable(m.definedKLabels()))); }
public org.kframework.definition.Definition loadDefinition( String mainModuleName, String syntaxModuleName, String definitionText, File source, File currentDirectory, List<File> lookupDirectories, boolean autoImportDomains, boolean kore) { return loadDefinition(mainModuleName, syntaxModuleName, definitionText, Source.apply(source.getAbsolutePath()), currentDirectory, lookupDirectories, autoImportDomains, kore); }
@Override public DebuggerMatchResult match(String pattern, String source) { String DebuggerSource = source; Rule compiledPattern = KRun.compilePattern(files, kem, pattern, compiledDef, Source.apply(DebuggerSource)); Rule parsedPattern = KRun.parsePattern(files, kem, pattern, compiledDef, Source.apply(DebuggerSource)); K subst = rewriter.match(getActiveState().getCurrentK(), compiledPattern); return new DebuggerMatchResult(subst, parsedPattern, compiledPattern, pattern); }
private Module parseModule(String def) { return ParserUtils.parseMainModuleOuterSyntax(def, Source.apply("generated by AddBracketsTest"), "TEST"); }
@Test public void testEmptyRules() throws Exception { // TODO: remove once the new parser is fully functional String def = "module TEST rule endmodule"; List<DefinitionItem> defItemList = Outer.parse(Source.apply("generated by OuterParsingTests"), def, null); Module mod = (Module) defItemList.get(0); StringSentence sen = (StringSentence) mod.getItems().get(0); Assert.assertEquals(sen.getContent(), ""); }
@Test public void testLexicalRules() throws Exception { // TODO: remove once the new parser is fully functional String def = "module TEST syntax Str ::= Token{((~[\\'\\n\\r\\\\])|([\\\\]~[\\n\\r]))*} endmodule"; List<DefinitionItem> defItemList = Outer.parse(Source.apply("generated by OuterParsingTests"), def, null); Module mod = (Module) defItemList.get(0); Syntax syn = (Syntax) mod.getItems().get(0); Lexical lex = (Lexical) syn.getPriorityBlocks().get(0).getProductions().get(0).getItems().get(0); Assert.assertEquals("((~[\\'\\n\\r\\\\])|([\\\\]~[\\n\\r]))*", lex.getLexicalRule()); }
private void parseRule(String input, String def, int warnings, boolean expectedError) { Module test = ParserUtils.parseMainModuleOuterSyntax(def, Source.apply("generated by RuleGrammarTest"), "TEST"); ParseInModule parser = RuleGrammarGenerator.getCombinedGrammar(gen.getRuleGrammar(test), true); Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> rule = parser.parseString(input, startSymbol, Source.apply("generated by RuleGrammarTest")); printout(rule, warnings, expectedError); }
@Test public void testLexicalRules2() throws Exception { // TODO: remove once the new parser is fully functional String def = "module TEST syntax Str ::= Token{{[a]|[b] \".\"}+NT~[x]*} endmodule"; List<DefinitionItem> defItemList = Outer.parse(Source.apply("generated by OuterParsingTests"), def, null); Module mod = (Module) defItemList.get(0); Syntax syn = (Syntax) mod.getItems().get(0); Lexical lex = (Lexical) syn.getPriorityBlocks().get(0).getProductions().get(0).getItems().get(0); Assert.assertEquals("{[a]|[b] \".\"}+NTDz~[x]*", lex.getLexicalRule()); } }
private void parseConfig(String input, String def, int warnings, boolean expectedError) { Module test = ParserUtils.parseMainModuleOuterSyntax(def, Source.apply("generated by RuleGrammarTest"), "TEST"); ParseInModule parser = RuleGrammarGenerator.getCombinedGrammar(gen.getConfigGrammar(test), true); Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> rule = parser.parseString(input, startSymbol, Source.apply("generated by RuleGrammarTest")); printout(rule, warnings, expectedError); }
private void parseProgram(String input, String def, String startSymbol, int warnings, boolean expectedError) { Module test = ParserUtils.parseMainModuleOuterSyntax(def, Source.apply("generated by RuleGrammarTest"), "TEST"); ParseInModule parser = RuleGrammarGenerator.getCombinedGrammar(gen.getProgramsGrammar(test), true); Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> rule = parser.parseString(input, Sort(startSymbol), Source.apply("generated by RuleGrammarTest")); printout(rule, warnings, expectedError); }
private void parseRule(String input, String def, int warnings, K expectedResult) { Module test = ParserUtils.parseMainModuleOuterSyntax(def, Source.apply("generated by RuleGrammarTest"), "TEST"); ParseInModule parser = RuleGrammarGenerator.getCombinedGrammar(gen.getRuleGrammar(test), true); Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> rule = parser.parseString(input, startSymbol, Source.apply("generated by RuleGrammarTest")); printout(rule, warnings, false); Assert.assertEquals(expectedResult, rule._1().right().get()); }
@Before public void setUp() throws Exception { RuleGrammarGenerator gen = makeRuleGrammarGenerator(); Module test = ParserUtils.parseMainModuleOuterSyntax(DEF, Source.apply("AddEmptyListsTest test definition"), "TEST"); parser = RuleGrammarGenerator.getCombinedGrammar(gen.getRuleGrammar(test), true); }
private void parseProgram(String input, String def, String startSymbol, int warnings, K expectedResult) { Module test = ParserUtils.parseMainModuleOuterSyntax(def, Source.apply("generated by RuleGrammarTest"), "TEST"); ParseInModule parser = RuleGrammarGenerator.getCombinedGrammar(gen.getProgramsGrammar(test), true); Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> rule = parser.parseString(input, Sort(startSymbol), Source.apply("generated by RuleGrammarTest")); printout(rule, warnings, false); Assert.assertEquals(expectedResult, rule._1().right().get()); }
private K parseTerm(String pgm, ParseInModule parser) { Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> result = parser.parseString(pgm, Sorts.K(), Source.apply("generated by AddBracketsTest")); assertEquals(0, result._2().size()); return new TreeNodesToKORE(Outer::parseSort, false).down(result._1().right().get()); }