예제 #1
0
        public static ParserState Parse(string source, IAbstractSyntaxTreePrinter printer = null, Rule rule = null)
        {
            if (rule == null)
                rule = _grammar.file;

            var state = new ParserState(source);
            try
            {
                if (rule.Match(state))
                {
                    if (state.AtEndOfInput())
                    {
                        if (printer != null)
                            printer.Print(state.GetRoot());

                        return state;
                    }

                    throw new ParsingException(state, rule, message: "Failed to read end of input");
                }

                throw new ParsingException(state, rule, message: "Failed to parse source");
            }
            catch (ParsingException e)
            {
                return state.Exception(e);
            }
        }
예제 #2
0
        public static ParserState Parse(string source, IAbstractSyntaxTreePrinter printer = null, Rule rule = null)
        {
            if (rule == null)
            {
                rule = _grammar.file;
            }

            var state = new ParserState(source);

            try
            {
                if (rule.Match(state))
                {
                    if (state.AtEndOfInput())
                    {
                        if (printer != null)
                        {
                            printer.Print(state.GetRoot());
                        }

                        return(state);
                    }

                    throw new ParsingException(state, rule, message: "Failed to read end of input");
                }

                throw new ParsingException(state, rule, message: "Failed to parse source");
            }
            catch (ParsingException e)
            {
                return(state.Exception(e));
            }
        }
예제 #3
0
        public static CppRipper.ParseNode GenerateCppAst(string cppContent, out string status)
        {
            CppStructuralGrammar grammar = new CppStructuralGrammar();
            Rule        parse_rule       = grammar.file;
            ParserState state            = new ParserState(cppContent);

            try
            {
                if (!parse_rule.Match(state))
                {
                    status = "Failed to parse c++ content";
                    return(null);
                }
                else
                {
                    if (state.AtEndOfInput())
                    {
                        status = "Successfully parsed c++ content";
                        return(state.GetRoot());
                    }
                    else
                    {
                        status = "Failed to read end of c++ content";
                        return(null);
                    }
                }
            }
            catch (ParsingException e)
            {
                state.ForceCompletion();
                status = e.Message;
                return(null);
            }
        }
예제 #4
0
        public void ParseConstant(string value, bool expected)
        {
            var grammer = new AsmBaseGrammar();
            var p       = new ParserState(value);

            Assert.That(grammer.constant.Match(p), Is.EqualTo(expected));

            ParseNode node = p.GetRoot();

            Console.WriteLine(node.Value);
        }
예제 #5
0
        public void Expression(string integer, bool expected)
        {
            var grammer = new AndyStructuralGrammar();
            var printer = new CppStructuralOutputAsXml();
            var p       = new ParserState(integer);

            Assert.That(grammer.expression.Match(p), Is.EqualTo(expected));

            printer.Print(p.GetRoot());
            Console.WriteLine(printer.AsXml());
        }
예제 #6
0
        public void Variable(string expression, bool expected)
        {
            var grammer = new AndyStructuralGrammar();
            var printer = new CppStructuralOutputAsXml();
            var p       = new ParserState(expression);

            Assert.That(grammer.identifier.Match(p), Is.EqualTo(expected));

            printer.Print(p.GetRoot());
            Console.WriteLine(printer.AsXml());
        }
예제 #7
0
        public void ParseLine(string value, bool expected)
        {
            var grammer = new AsmStructuralGrammar();
            var p       = new ParserState(value);

            Assert.That(grammer.line.Match(p), Is.EqualTo(expected));

            var printer = new CppStructuralOutputAsXml();

            printer.Print(p.GetRoot());
            Console.WriteLine(printer.AsXml());
        }
예제 #8
0
        public void ParseOpCode(string value, bool expected)
        {
            var grammer = new AsmBaseGrammar();
            var printer = new CppStructuralOutputAsXml();
            var p       = new ParserState(value);

            Assert.That(grammer.opcode.Match(p), Is.EqualTo(expected));

            ParseNode node = p.GetRoot();

            Console.WriteLine(node.Value);
        }
예제 #9
0
        public Assembler(string source) : this()
        {
            var  parser = new ParserState(source);
            bool match  = _grammar.program.Match(parser);

            if (match == false)
            {
                throw new Exception("Cannot parse source file");
            }

            _root = parser.GetRoot();
        }
예제 #10
0
파일: Compiler.cs 프로젝트: andy-uq/TinyOS
        public Compiler(AndyStructuralGrammar grammar, ParserState parserState)
        {
            _grammar = grammar;

            var rules = grammar.GetRules <AndyStructuralGrammar>(throwOnMissing: false);

            rules = rules.Concat(grammar.GetRules <AndyBaseGrammar>(throwOnMissing: false));

            _codeGenerator = rules
                             .Distinct()
                             .ToDictionary(k => k, GetCodeGenerator);

            _root = parserState.GetRoot();
        }
예제 #11
0
        public void OpenFile(string file)
        {
            var grammar = new AsmStructuralGrammar();

            file = string.Concat(_testPaths[Environment.MachineName], file);

            var outputFile = Path.ChangeExtension(file, ".asm");

            using (var stringWriter = File.CreateText(outputFile))
                using (var writer = new InstructionTextWriter(stringWriter))
                {
                    string source = File.ReadAllText(file);
                    var    parser = new ParserState(source);
                    var    result = grammar.program.Match(parser);
                    Assert.That(result, Is.True);

                    var printer = new CppStructuralOutputAsXml();
                    printer.Print(parser.GetRoot());
                    Console.WriteLine(printer.AsXml());

                    var assembler = new Assembler(parser);
                    assembler.Assemble().ToList().ForEach(writer.Write);
                }
        }
예제 #12
0
 public Assembler(ParserState parser) : this()
 {
     _root = parser.GetRoot();
 }