Ejemplo n.º 1
0
        static int ParseFiles(string grammarFile, string rootParserName, List <string> sourceFiles, string outputFormat)
        {
            if (string.IsNullOrEmpty(grammarFile))
            {
                WriteError("Expected a grammar file path");
                return(1);
            }

            Parser parser = new ExtendedBackusNaurFormParser();

            if (!TryParse(parser, grammarFile))
            {
                return(1);
            }

            try {
                parser = ParserGenerator.FromEBnf(File.ReadAllText(grammarFile), rootParserName);
            } catch (ParserGenerationException e) {
                foreach (var semanticError in e.Errors)
                {
                    WriteError("{0}({2},{3}): {1}", grammarFile, semanticError.Message, semanticError.LineNumber, semanticError.ColumnNumber);
                }
            }

            var error = false;

            foreach (var file in sourceFiles)
            {
                error |= TryParse(parser, file, outputFormat);
            }

            return(error ? 1 : 0);
        }
Ejemplo n.º 2
0
        static ReflectionCommands()
        {
            const string ebnf = @"
                (* skip-whitespace omit-from-hierarchy *)
                Location Root = Location, End of Input;

                (* skip-whitespace omit-from-hierarchy *)
                Value Root = Value, End of Input;

                Location = ""["", Root, ""]"", {Accessor};

                (* omit-from-hierarchy *) Root     = Networkable Identifier | Type Name;
                (* omit-from-hierarchy *) Accessor = Member Access | Component Access | Invocation;

                Member Access = ""."", Identifier;
                Component Access = ""<"", Type Name, "">"";

                Invocation = ""("", [Value, {"","", Value}], "")"";

                (* omit-from-hierarchy *)
                Value = Null | Boolean | Vector | Float | Double | (""0x"", Hex Integer) | Integer | String | Location;

                Boolean = True | False;
                Vector = ""Vector"", Invocation;

                Float = (Integer | Decimal), ""f"";
                Double = Decimal, [""d""];

                String = ""'"", {(""\\"", Escaped Character) | Single Character}, ""'"";

                (* collapse *) End of Input = ? /$/ ?;
                (* collapse *) Single Character = ? /[^\\']/ ?;
                (* collapse *) Escaped Character = ? /[\\'rnt]/ ?;
                (* collapse *) Integer = ? /-?[0-9]+/ ?;
                (* collapse *) Hex Integer = ? /[0-9A-F]/i ?;
                (* collapse *) Decimal = ? /-?[0-9]+\.[0-9]+/ ?;

                (* collapse *) Networkable Identifier = ? /[0-9]+/ ?;
                (* collapse *) Type Name = ? /[^0-9\]>][^\]>]*/ ?;
                (* collapse *) Component Name = Type Name;
                (* collapse *) Null = ""null"";
                (* collapse *) True = ? /[Tt]rue/ ?;
                (* collapse *) False = ? /[Ff]alse/ ?;
                (* collapse *) Identifier = ? /[A-Za-z_][A-Za-z0-9_]*/ ?;";

            _sLocationParser = ParserGenerator.FromEBnf(ebnf, "Location Root");
            _sValueParser    = ParserGenerator.FromEBnf(ebnf, "Value Root");
        }
Ejemplo n.º 3
0
        private static IEnumerable <string[]> Split(String args)
        {
            if (_sCommandListParser == null)
            {
                _sCommandListParser = ParserGenerator.FromEBnf(@"
                    (* skip-whitespace *)
                    Command List      = Command, {"";"", Command}, End of Input;
                    Command           = Argument, {Argument};

                    (* collapse *)
                    End of Input      = ? /$/ ?;

                    (* match-whitespace *)
                    Argument          = Argument Part, {Argument Part};

                    (* omit-from-hierarchy *)
                    Argument Part     = Simple Character | (""\\"", Escaped Character) | (""\"""", Quoted String, ""\"""");

                    (* omit-from-hierarchy *)
                    Quoted String     = {Quoted Character | (""\\"", Escaped Character)};

                    (* collapse *)
                    Simple Character  = ? /[^\s\\"";]/ ?;

                    (* collapse *)
                    Quoted Character  = ? /[^""\\]/ ?;

                    (* collapse *)
                    Escaped Character = ""\\"" | ""\"""" | ""n"" | ""t"" | ""r"";
                ");
            }

            var match = _sCommandListParser.Parse(args);

            if (!match.Success)
            {
                throw new Exception(match.Error.ToString());
            }

            return(match
                   .Where(x => x.Parser.Name == "Command")
                   .Select(command => command
                           .Select(arg => String.Join(String.Empty, arg
                                                      .Select(c => {
                if (c.Parser.Name != "Escaped Character")
                {
                    return c.Value;
                }
                switch (c.Value[0])
                {
                case '\\': return "\\";

                case 'n': return "\n";

                case 'r': return "\r";

                case 't': return "\t";
                }
                return c.Value;
            })
                                                      .ToArray()))
                           .ToArray()));
        }