public CatMetaDataBlock(AstMetaDataBlock node) : base("root", null) { CatMetaData cur = this; int nCurIndent = -1; for (int i = 0; i < node.children.Count; ++i) { AstMetaData tmp = node.children[i]; if (tmp is AstMetaDataLabel) { int nIndent; string sName; SplitLabel(tmp.ToString(), out nIndent, out sName); if (nIndent > nCurIndent) { cur = cur.NewChild(sName); } else if (nIndent == nCurIndent) { cur = cur.GetParent(); Trace.Assert(cur != null); cur = cur.NewChild(sName); } else { cur = cur.GetParent(); Trace.Assert(cur != null); cur = cur.GetParent(); Trace.Assert(cur != null); cur = cur.NewChild(sName); } nCurIndent = nIndent; } else if (tmp is AstMetaDataContent) { cur.AddContent(tmp.ToString()); } else { throw new Exception("invalid AstMetaDataBlock"); } } }
public static CatMetaDataBlock Create(string s) { s = "{{\n" + s + "\n}}\n"; Peg.Parser parser = new Peg.Parser(s); bool bResult = parser.Parse(CatGrammar.MetaDataBlock()); if (!bResult) { throw new Exception("failed to parse meta-data block"); } Peg.PegAstNode tree = parser.GetAst(); if (tree.GetNumChildren() == 0) { return(null); } if (tree.GetNumChildren() != 1) { throw new Exception("invalid number of child nodes in meta-data block node"); } AstMetaDataBlock node = new AstMetaDataBlock(tree.GetChild(0)); CatMetaDataBlock ret = new CatMetaDataBlock(node); return(ret); }
public AstDef(PegAstNode node) : base(node) { CheckLabel(AstLabel.Def); if (node.GetNumChildren() == 0) { throw new Exception("invalid function definition node"); } AstName name = new AstName(node.GetChild(0)); mName = name.ToString(); int n = 1; // Look to see if a type is defined if ((node.GetNumChildren() >= 2) && (node.GetChild(1).GetLabel().Equals(AstLabel.FxnType))) { mType = new AstFxnType(node.GetChild(1)); ++n; } while (n < node.GetNumChildren()) { PegAstNode child = node.GetChild(n); if (!child.GetLabel().Equals(AstLabel.Param)) { break; } mParams.Add(new AstParam(child)); n++; } while (n < node.GetNumChildren()) { PegAstNode child = node.GetChild(n); if (!child.GetLabel().Equals(AstLabel.Param)) { break; } mParams.Add(new AstParam(child)); n++; } while (n < node.GetNumChildren()) { PegAstNode child = node.GetChild(n); if (!child.GetLabel().Equals(AstLabel.MetaDataBlock)) { break; } mpMetaData = new AstMetaDataBlock(child); n++; } while (n < node.GetNumChildren()) { PegAstNode child = node.GetChild(n); if (!child.GetLabel().Equals(AstLabel.Def)) { break; } mLocals.Add(new AstDef(child)); n++; } while (n < node.GetNumChildren()) { PegAstNode child = node.GetChild(n); CatAstNode expr = Create(child); if (!(expr is AstExpr)) { throw new Exception("expected expression node"); } mTerms.Add(expr as AstExpr); n++; } }
public AstDef(PegAstNode node) : base(node) { CheckLabel(AstLabel.Def); if (node.GetNumChildren() == 0) throw new Exception("invalid function definition node"); AstName name = new AstName(node.GetChild(0)); mName = name.ToString(); int n = 1; // Look to see if a type is defined if ((node.GetNumChildren() >= 2) && (node.GetChild(1).GetLabel().Equals(AstLabel.FxnType))) { mType = new AstFxnType(node.GetChild(1)); ++n; } while (n < node.GetNumChildren()) { PegAstNode child = node.GetChild(n); if (!child.GetLabel().Equals(AstLabel.Param)) break; mParams.Add(new AstParam(child)); n++; } while (n < node.GetNumChildren()) { PegAstNode child = node.GetChild(n); if (!child.GetLabel().Equals(AstLabel.Param)) break; mParams.Add(new AstParam(child)); n++; } while (n < node.GetNumChildren()) { PegAstNode child = node.GetChild(n); if (!child.GetLabel().Equals(AstLabel.MetaDataBlock)) break; mpMetaData = new AstMetaDataBlock(child); n++; } while (n < node.GetNumChildren()) { PegAstNode child = node.GetChild(n); if (!child.GetLabel().Equals(AstLabel.Def)) break; mLocals.Add(new AstDef(child)); n++; } while (n < node.GetNumChildren()) { PegAstNode child = node.GetChild(n); CatAstNode expr = Create(child); if (!(expr is AstExpr)) throw new Exception("expected expression node"); mTerms.Add(expr as AstExpr); n++; } }