本文整理汇总了C#中Irony.Parsing.Parser类的典型用法代码示例。如果您正苦于以下问题:C# Parser类的具体用法?C# Parser怎么用?C# Parser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Parser类属于Irony.Parsing命名空间,在下文中一共展示了Parser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldNotParseCaseElseWhenNotAtTheEnd
public void ShouldNotParseCaseElseWhenNotAtTheEnd()
{
// Arrange
var grammar = new CicodeGrammar();
var parser = new Parser(grammar);
var sourceCode =
@"
FUNCTION A()
SELECT CASE a
CASE 1
A();
CASE ELSE
A();
CASE 1
A();
END SELECT
END
";
// Act
var parseTree = parser.Parse(sourceCode);
// Assert
Assert.IsNotNull(parseTree);
Assert.IsTrue(parseTree.HasErrors());
// A parser error is expected at line 7 because of the CASE ELSE clause which is not at then end of the case list
Assert.AreEqual<int>(1, parseTree.ParserMessages.Where(m => m.Location.Line == 7).Count());
}
示例2: Read
public override object Read(string path)
{
string txt;
using (var reader = new StreamReader(path))
{
txt = reader.ReadToEnd();
}
var grammar = new OpenFoamGrammar();
var parser = new Parser(grammar);
var tree = parser.Parse(txt);
var d = new Vertice();
foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
{
var identifier = rootEntryNode.GetEntryIdentifier();
switch (identifier)
{
case "value":
d.X = rootEntryNode.GetDictArrayBody().GetArrayOfDecimal()[0];
d.Y = rootEntryNode.GetDictArrayBody().GetArrayOfDecimal()[1];
d.Z = rootEntryNode.GetDictArrayBody().GetArrayOfDecimal()[2];
break;
}
}
return d;
}
示例3: Read
public override object Read(string path)
{
var rawData = new FvSolutionData();
string txt;
using (var reader = new StreamReader(path))
{
txt = reader.ReadToEnd();
}
var grammar = new OpenFoamGrammar();
var parser = new Parser(grammar);
var tree = parser.Parse(txt);
foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
{
var identifier = rootEntryNode.GetEntryIdentifier();
switch (identifier)
{
case "options":
ParseOptions(rootEntryNode.ChildNodes[2], rawData);
break;
case "solvers":
ParseSolvers(rootEntryNode.ChildNodes[2], rawData);
break;
}
}
return rawData;
}
示例4: TestConflictGrammarWithHintsOnRules
public void TestConflictGrammarWithHintsOnRules()
{
var grammar = new ConflictGrammarWithHintsInRules();
var parser = new Parser(grammar);
Assert.True(parser.Language.Errors.Count == 0);
// Field sample
var sample = FieldSample;
var tree = parser.Parse(sample);
Assert.NotNull(tree);
Assert.False(tree.HasErrors());
Assert.NotNull(tree.Root);
var term = tree.Root.Term as NonTerminal;
Assert.NotNull(term);
Assert.Equal("definition", term.Name);
Assert.Equal(1, tree.Root.ChildNodes.Count);
var modNode = tree.Root.ChildNodes[0].ChildNodes[0];
Assert.Equal("fieldModifier", modNode.Term.Name);
//Property
sample = PropertySample;
tree = parser.Parse(sample);
Assert.NotNull(tree);
Assert.False(tree.HasErrors());
Assert.NotNull(tree.Root);
term = tree.Root.Term as NonTerminal;
Assert.NotNull(term);
Assert.Equal("definition", term.Name);
Assert.Equal(1, tree.Root.ChildNodes.Count);
modNode = tree.Root.ChildNodes[0].ChildNodes[0];
Assert.Equal("propModifier", modNode.Term.Name);
}
示例5: ExpressionEvaluator
//Default constructor, creates default evaluator
public ExpressionEvaluator(ExpressionEvaluatorGrammar grammar) {
Grammar = grammar;
Language = new LanguageData(Grammar);
Parser = new Parser(Language);
Runtime = Grammar.CreateRuntime(Language);
App = new ScriptApp(Runtime);
}
示例6: Read
public override object Read(string path)
{
var obj = new AirfoilPropertiesInstance();
string text = Load(path);
var grammar = new OpenFoamGrammar();
var parser = new Parser(grammar);
var tree = parser.Parse(text);
obj.row = new List<Vertice>();
foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
{
var id = rootEntryNode.GetEntryIdentifier();
if (id == "airfoilData")
{
var dict = rootEntryNode.GetDictContent().ChildNodes[1];
foreach (ParseTreeNode t in dict.ChildNodes)
{
var array_head = t.ChildNodes[0].ChildNodes[1].ChildNodes;
var v = new Vertice
{
X = Convert.ToDecimal(array_head[0].ChildNodes[0].Token.Value),
Y = Convert.ToDecimal(array_head[1].ChildNodes[0].Token.Value),
Z = Convert.ToDecimal(array_head[2].ChildNodes[0].Token.Value)
};
obj.row.Add( v );
}
}
}
obj.airfoilName = FileName;
return obj;
}
示例7: Read
public override object Read(string path)
{
var obj = new RefineMeshDictData();
string txt;
using (var reader = new StreamReader(path))
{
txt = reader.ReadToEnd();
}
var grammar = new OpenFoamGrammar();
var parser = new Parser(grammar);
var tree = parser.Parse(txt);
foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
{
var identifier = rootEntryNode.GetEntryIdentifier();
string patch;
switch ( identifier )
{
case "set":
obj.setvalue = rootEntryNode.GetBasicValString();
break;
case "coordinateSystem":
obj.coordsys = rootEntryNode.GetBasicValEnum<CoordinateSystem>();
break;
case "globalCoeffs":
{
var dict = rootEntryNode.GetDictContent();
obj.globalCoeffs = GetCoeffs(ref dict, out patch);
}
break;
case "patchLocalCoeffs":
{
var dict = rootEntryNode.GetDictContent();
obj.patchLocalCoeffs = GetCoeffs(ref dict, out patch);
obj.patch = patch;
}
break;
case "directions":
{
obj.direction = new List<DirectionType>();
var s = rootEntryNode.ChildNodes[2].ChildNodes[1].GetArrayOfString();
foreach (string t in s)
{
obj.direction.Add(t.ToEnum<DirectionType>());
}
}
break;
case "useHexTopology":
obj.useHexTopology = rootEntryNode.GetBasicValBool();
break;
case "geometricCut":
obj.geometricCut = rootEntryNode.GetBasicValBool();
break;
case "writeMesh":
obj.writeMesh = rootEntryNode.GetBasicValBool();
break;
}
}
return obj;
}
示例8: FSharpParser
public FSharpParser()
: base()
{
grammar = new FSharpGrammar();
parser = new Parser(new LanguageData(grammar));
}
示例9: Read
public override object Read(string path)
{
var obj = new DecomposeParDictData();
string txt = Load(path);
var grammar = new OpenFoamGrammar();
var parser = new Parser(grammar);
var tree = parser.Parse(txt);
foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
{
var identifier = rootEntryNode.GetEntryIdentifier();
switch (identifier)
{
case "numberOfSubdomains":
obj.numberOfSubdomains = rootEntryNode.GetBasicValInt();
break;
case "method":
obj.method = rootEntryNode.GetBasicValEnum<DecompositionMethod>();
break;
case "hierarchicalCoeffs":
obj.hCoefs = GetHierarchicalCoeffs(rootEntryNode.GetDictContent());
break;
}
}
return obj;
}
示例10: Read
public override object Read(string path)
{
var obj = new AirfoilPropertiesData();
string text;
using (var reader = new StreamReader(path))
{
text = reader.ReadToEnd();
}
var grammar = new OpenFoamGrammar();
var parser = new Parser(grammar);
var tree = parser.Parse(text);
obj.airfoilData = new List<Vertice>();
foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
{
var id = rootEntryNode.GetEntryIdentifier();
if (id == "airfoilData")
{
var dict = rootEntryNode.GetDictContent().ChildNodes[1];
for (int i = 0; i < dict.ChildNodes.Count; i++)
{
var array_head = dict.ChildNodes[i].ChildNodes[0].ChildNodes[1].ChildNodes;
var v = new Vertice();
v.X = Convert.ToDecimal(array_head[0].ChildNodes[0].Token.Value);
v.Y = Convert.ToDecimal(array_head[1].ChildNodes[0].Token.Value);
v.Z = Convert.ToDecimal(array_head[2].ChildNodes[0].Token.Value);
obj.airfoilData.Add( v );
}
}
}
return obj;
}
示例11: NoGrammarConflicts
// Ensure that the grammar has no conflicts
public void NoGrammarConflicts()
{
/* Conflict either indicate an ambiguity in the grammar or an error in the rules
* In case of an ambiguity Irony will choose an action itself, which is not always the correct one
* Rewrite the grammar rules untill there are no conflicts, and the parses are corrent
* As a very last resort, you can manually specify shift with a grammar hint like PreferShiftHere() or ReduceHere()
* However, this should only be done if one of the two is always correct, otherwise the grammar will need to be written differently
*/
/* An example:
* (A1) can be parsed both as an union and a bracketed reference
* This is an ambiguity, and thus must be solved by precedence or a grammar hint
*/
/* An example:
* Functioncall.Rule =
* Prefix + Formula // Prefix unop
* | Formula + infix + Formula // Binop
* | Formula + Formula // Intersection
*
* With this 1+1 can be parsed as both 1-1 and 1 (-1)
* This is obviously erroneous as there is only 1 correct interpertation, so the rules had to be rewritten.
*/
var parser = new Parser(new ExcelFormulaGrammar());
Assert.IsTrue(parser.Language.Errors.Count == 0, "Grammar has {0} error(s) or conflict(s)", parser.Language.Errors.Count);
}
示例12: Read
public override object Read(string path)
{
var rawData = new DecomposeParDictData();
string txt;
using (var reader = new StreamReader(path))
{
txt = reader.ReadToEnd();
}
var grammar = new OpenFoamGrammar();
var parser = new Parser(grammar);
var tree = parser.Parse(txt);
foreach (ParseTreeNode rootEntryNode in tree.Root.FindDictEntries(null))
{
var identifier = rootEntryNode.GetEntryIdentifier();
switch (identifier)
{
case "numberOfSubdomains":
rawData.numberOfSubdomains = rootEntryNode.GetBasicValInt();
break;
}
}
return rawData;
}
示例13: LoadBnfFile
private static void LoadBnfFile(string fileName, Builder mainBuilder)
{
Console.WriteLine("Parse BNF file: {0}", fileName);
var bnf = File.ReadAllText(fileName);
var metaParser = new MetaParser();
var meta = metaParser.Parse(bnf);
var oprimizedBnf = Optimize(bnf);
var parser = new Parser(new BnfGrammar(meta.Mode));
var tree = parser.Parse(oprimizedBnf, fileName);
if (tree.Status == ParseTreeStatus.Error)
{
throw new Exception((tree.ParserMessages.Count > 0)
? string.Format("{0}, in {3} file at line {1}, column {2}", tree.ParserMessages[0].Message, tree.ParserMessages[0].Location.Line, tree.ParserMessages[0].Location.Column, fileName)
: string.Format(@"Unknow error in BNF file {0}", fileName));
}
var builder = new Builder(tree, mainBuilder);
builder.BuildExpressions();
foreach (var @using in meta.Usings)
LoadBnfFile(@using, mainBuilder);
}
示例14: ShouldNotParseWithoutCases
public void ShouldNotParseWithoutCases()
{
// Arrange
var grammar = new CicodeGrammar();
var parser = new Parser(grammar);
var sourceCode =
@"
FUNCTION A()
SELECT CASE a
END SELECT
END
";
// Act
var parseTree = parser.Parse(sourceCode);
// Assert
Assert.IsNotNull(parseTree);
Assert.IsTrue(parseTree.HasErrors());
// A parser error is expected at the end of line 4 because the expected list of cases which is missing
Assert.AreEqual<int>(1, parseTree.ParserMessages
.Where(m => m.Location.Line == 4)
.Where(m => m.ParserState.ExpectedTerminals.First().Name == "CASE")
.Count());
}
示例15: TestCase
public void TestCase()
{
GLSLGrammar lang = new GLSLGrammar ();
var compiler = new Irony.Parsing.Parser(lang);
var tree = compiler.Parse ("void main(void) { float v = vec3(1,1,1); }");
//CheckNodes (tree.Root, 0);
}