Esempio n. 1
0
        /// <summary>
        /// 使用指定的语法规则初始化 <see cref="Cyjb.Compilers.Lexers.LexerRule&lt;T&gt;"/> 类的新实例。
        /// </summary>
        /// <param name="grammar">词法分析器使用的语法规则。</param>
        internal LexerRule(Grammar <T> grammar)
        {
            if (grammar == null)
            {
                throw CommonExceptions.ArgumentNull("grammar");
            }
            this.contextCount = grammar.Contexts.Count;
            this.contexts     = new Dictionary <string, int>(contextCount);
            int i = 0;

            foreach (string context in grammar.Contexts)
            {
                this.contexts.Add(context, i++);
            }
            this.symbolCount = grammar.Terminals.Count;
            this.symbols     = new SymbolData <T> [this.symbolCount];
            foreach (Terminal <T> sym in grammar.Terminals)
            {
                this.symbols[sym.Index] = new SymbolData <T>(sym.Id, sym.Action);
            }
            FillEOFActions(grammar);
            bool useTrailing;

            FillDfa(grammar, out useTrailing);
            if (useTrailing)
            {
                FillTrailing(grammar);
            }
            else
            {
                this.trailingType = TrailingType.None;
            }
            this.ContainsBeginningOfLineHeader = true;
        }
Esempio n. 2
0
        /// <summary>
        /// 填充行首匹配和向前看的数据。
        /// </summary>
        /// <param name="grammar">词法分析器使用的语法。</param>
        private void FillTrailing(Grammar <T> grammar)
        {
            bool variableTrailing = false;

            this.containsBeginningOfLineRule = false;
            foreach (Terminal <T> sym in grammar.Terminals)
            {
                AnchorExp exp = sym.RegularExpression as AnchorExp;
                if (exp != null)
                {
                    if (exp.BeginningOfLine == true)
                    {
                        this.containsBeginningOfLineRule = true;
                    }
                    if (exp.TrailingExpression != null)
                    {
                        int len = exp.TrailingExpression.Length;
                        if (len != -1)
                        {
                            this.symbols[sym.Index].Trailing = -len;
                        }
                        else
                        {
                            len = exp.InnerExpression.Length;
                            if (len != -1)
                            {
                                this.symbols[sym.Index].Trailing = len;
                            }
                            else
                            {
                                this.symbols[sym.Index].Trailing = 0;
                                variableTrailing = true;
                            }
                        }
                    }
                }
            }
            if (variableTrailing)
            {
                this.trailingType = TrailingType.Variable;
            }
            else
            {
                this.trailingType = TrailingType.Fixed;
            }
        }