예제 #1
0
        /// <summary>
        /// Creates a new grammar, replacing unknownTypeTokens with either rule types or token types
        /// </summary>
        public Productions Compile(Lexer t)
        {
            Productions compiled = new Productions();

            compiled.AddRange(this.Select(r => r.DeepCopy()));
            Compile(compiled, compiled, t);
            return(compiled);
        }
예제 #2
0
 private void Compile(IEnumerable <Rule> rules, Productions g, Lexer t)
 {
     foreach (Rule rule in rules)
     {
         if (rule is Grouping)
         {
             Compile((Grouping)rule, g, t);
         }
         else if (rule is Referrer)
         {
             ((Referrer)rule).CreateReference(g, t);
         }
     }
 }
예제 #3
0
        static private void ProcessSection(SyntaxNode section, Parser parser)
        {
            if (section.Rule.ProductionName != "section")
            {
                throw Exceptions.InvalidSyntaxException(section, "section");
            }

            string name  = null;
            string value = null;

            foreach (SyntaxNode node in section)
            {
                switch (node.Rule.ProductionName)
                {
                case "name":
                    name = node.StartToken.Text.Trim();
                    break;

                case "value":
                    value = node.StartToken.Text.Trim();
                    break;
                }
            }

            switch (name)
            {
            case "#AlternateCritera":
                parser.Settings.AlternateCritera = (ParserSettings.SelectionCriteria)
                                                   Enum.Parse(typeof(ParserSettings.SelectionCriteria), value);
                break;

            case "#RootProductionNames":
                parser.Settings.RootProductionNames = new List <string>(
                    Regex.Split(value, @"\s*,\s*"));
                break;

            case "#ZeroLengthRulesOK":
                // boolean.parse wasn't working.
                parser.Settings.ZeroLengthRulesOK =
                    Regex.Match(value, @"\btrue\b", RegexOptions.IgnoreCase).Success;
                break;

            case "#RegexOptions":
                parser.Lexer.RegexOptions = RegexOptions.None;
                if (value.Contains("x"))
                {
                    parser.Lexer.RegexOptions |= RegexOptions.IgnorePatternWhitespace;
                }
                if (value.Contains("i"))
                {
                    parser.Lexer.RegexOptions |= RegexOptions.IgnoreCase;
                }
                if (value.Contains("m"))
                {
                    parser.Lexer.RegexOptions |= RegexOptions.Multiline;
                }
                if (value.Contains("s"))
                {
                    parser.Lexer.RegexOptions |= RegexOptions.Singleline;
                }
                if (value.Contains("n"))
                {
                    parser.Lexer.RegexOptions |= RegexOptions.ExplicitCapture;
                }
                if (value.Contains("r"))
                {
                    parser.Lexer.RegexOptions |= RegexOptions.RightToLeft;
                }
                if (value.Contains("none"))
                {
                    parser.Lexer.RegexOptions = RegexOptions.None;
                }
                break;

            case "#TokenSubs":
                parser.Lexer.Substitutions = DefinitionCollection.Parse(value);
                break;

            case "#Tokens":
                parser.Lexer.Modes.Clear();
                parser.Lexer.Modes.Add("default", DefinitionCollection.Parse(value));
                break;

            case "#Productions":
                parser.Productions = Productions.Parse(value);
                break;
            }
        }
예제 #4
0
 public Parser(ParserSettings settings = null)
 {
     Productions = new Productions();
     Lexer       = new Lexer();
     Settings    = settings ?? new ParserSettings();
 }