示例#1
0
        public static CatFxnType Create(string sType)
        {
            if (sType.Length == 0)
            {
                return(null);
            }

            Peg.Parser p = new Peg.Parser(sType);
            try {
                if (!p.Parse(CatGrammar.FxnType()))
                {
                    throw new Exception("no additional information");
                }
            } catch (Exception e) {
                throw new Exception(sType + " is not a valid function type ", e);
            }

            Peg.PegAstNode ast = p.GetAst();
            if (ast.GetNumChildren() != 1)
            {
                throw new Exception("invalid number of children in abstract syntax tree");
            }
            AstFxnType node = new AstFxnType(ast.GetChild(0));
            CatFxnType ret  = new CatFxnType(node);

            return(ret);
        }
示例#2
0
        public static List <CatAstNode> Parse(string s)
        {
            Peg.Parser parser = new Peg.Parser(s);

            try {
                bool bResult = parser.Parse(CatGrammar.CatProgram());
                if (!bResult)
                {
                    throw new Exception("failed to parse input");
                }
            } catch (Exception e) {
                Output.WriteLine("Parsing error occured with message: " + e.Message);
                Output.WriteLine(parser.ParserPosition);
                throw e;
            }

            AstProgram tmp = new AstProgram(parser.GetAst());

            return(tmp.mStatements);
        }
示例#3
0
        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);
        }