Esempio n. 1
0
        /// <summary>
        /// source = {definition|"Comment"} ;
        /// </summary>
        /// <param name="repetition">{definition|"Comment"}</param>
        protected override SourceElement ReturnSource(Selection <DefinitionElement, GeneratorLexElement>[] repetition)
        {
            SourceElement ret = new SourceElement();
            Dictionary <string, List <DefinitionElement> > defSet = new Dictionary <string, List <DefinitionElement> >();

            foreach (var selection in repetition)
            {
                if (selection.Element1.HasValue)
                {
                    List <DefinitionElement> defList;
                    if (!defSet.TryGetValue(selection.Element1.Value.DefinitionName, out defList))
                    {
                        defSet[selection.Element1.Value.DefinitionName] = defList = new List <DefinitionElement>();
                    }
                    defList.Add(selection.Element1.Value);
                }
                else
                {
                    Debug.Assert(selection.Element2.HasValue);
                    ret.Comments.Add(selection.Element2.Value);
                }
            }
            foreach (List <DefinitionElement> defs in defSet.Values)
            {
                Debug.Assert(defs.Count > 0);
                List <ElementsElement> elements = new List <ElementsElement>();
                foreach (DefinitionElement def in defs)
                {
                    elements.AddRange(def.Expression.Selection.Candidates);
                }
                SelectionElement select = new SelectionElement(elements);
                ret.Defs.Add(new DefinitionElement(defs.First().Left, new ExpressionsElement(select)));
            }
            return(ret);
        }
        void firstFollowing(ScriptParserGenerator gen, DefinitionElement root, TextWriter writer, DefinitionContent content)
        {
            writer.WriteLine(string.Format("   {0}: first:<{1}> following:<{2}>", content, joinStr(content.GetFirstTerminals(gen)), joinStr(content.GetFollowingTerminals(gen))));
            SelectionElement s = content as SelectionElement;

            if (s != null)
            {
                foreach (ElementsElement elems in s.Candidates)
                {
                    firstFollowing(gen, root, writer, elems);
                }
            }
            ExpressionsElement ex = content as ExpressionsElement;

            if (ex != null)
            {
                foreach (ElementsElement elems in ex.Selection.Candidates)
                {
                    firstFollowing(gen, root, writer, elems);
                }
            }
            ElementsElement es = content as ElementsElement;

            if (es != null)
            {
                foreach (ElementElement elem in es.Elements)
                {
                    firstFollowing(gen, root, writer, elem);
                }
            }
            RepeatElement r = content as RepeatElement;

            if (r != null)
            {
                firstFollowing(gen, root, writer, r.InnerExpression);
            }
            OptionElement o = content as OptionElement;

            if (o != null)
            {
                firstFollowing(gen, root, writer, o.InnerExpression);
            }
            GroupElement g = content as GroupElement;

            if (g != null)
            {
                firstFollowing(gen, root, writer, g.InnerExpression);
            }
            LiteralElement l = content as LiteralElement;
        }
        public int GetHashCode(DefinitionContent obj)
        {
            SelectionElement s = obj as SelectionElement;

            if (s != null)
            {
                // ハッシュ値計算できないので適当
                return(15773);
            }
            ExpressionsElement ex = obj as ExpressionsElement;

            if (ex != null)
            {
                return(this.GetHashCode(ex.Selection));
            }
            ElementsElement es = obj as ElementsElement;

            if (es != null)
            {
                return(es.Elements.Sum(e => this.GetHashCode(e)));
            }
            RepeatElement r = obj as RepeatElement;

            if (r != null)
            {
                return(1744579 + this.GetHashCode(r.InnerExpression));
            }
            OptionElement o = obj as OptionElement;

            if (o != null)
            {
                return(3234551 + this.GetHashCode(o.InnerExpression));
            }
            GroupElement g = obj as GroupElement;

            if (g != null)
            {
                return(this.GetHashCode(g.InnerExpression));
            }
            LiteralElement l = obj as LiteralElement;

            if (l != null)
            {
                return(l.InnerWord.GetHashCode() + l.Literal.Type.GetHashCode());
            }
            Debug.Assert(false);
            return(7);
        }
 /// <summary>
 /// expressions = selection ;
 /// </summary>
 /// <param name="selection">selection</param>
 protected abstract ExpressionsElement ReturnExpressions(SelectionElement selection);
        protected ExpressionsElement ParseExpressions()
        {
            SelectionElement var1 = this.ParseSelection();

            return(this.ReturnExpressions(var1));
        }
 public ExpressionsElement(SelectionElement selection)
 {
     _selection = selection;
 }
        public bool Equals(DefinitionContent x, DefinitionContent y)
        {
            GroupElement x1 = x as GroupElement;

            if (x1 != null)
            {
                return(this.Equals(x1.InnerExpression, y));
            }
            GroupElement y1 = y as GroupElement;

            if (y1 != null)
            {
                return(this.Equals(x, y1.InnerExpression));
            }
            ExpressionsElement x2 = x as ExpressionsElement;

            if (x2 != null)
            {
                return(this.Equals(x2.Selection, y));
            }
            ExpressionsElement y2 = y as ExpressionsElement;

            if (y2 != null)
            {
                return(this.Equals(x, y2.Selection));
            }
            SelectionElement x3 = x as SelectionElement;
            SelectionElement y3 = y as SelectionElement;

            if (x3 != null && y3 != null)
            {
                // ∀x0(∈ x) x0 ∈ y かつ ∀y0(∈ y) y0 ∈ x ならば x = y
                return(x3.Candidates.All(x0 => y3.Candidates.Any(y0 => this.Equals(x0, y0))) && y3.Candidates.All(y0 => x3.Candidates.Any(x0 => this.Equals(x0, y0))));
            }
            ElementsElement x4 = x as ElementsElement;
            ElementsElement y4 = y as ElementsElement;

            if (x4 != null && y4 != null)
            {
                if (x4.Elements.Count != y4.Elements.Count)
                {
                    return(false);
                }
                return(CollectionEx.Zip(x4.Elements, y4.Elements, (x0, y0) => this.Equals(x0, y0)).All(eq => eq));
            }
            RepeatElement x5 = x as RepeatElement;
            RepeatElement y5 = y as RepeatElement;

            if (x5 != null && y5 != null)
            {
                return(this.Equals(x5.InnerExpression, y5.InnerExpression));
            }
            OptionElement x6 = x as OptionElement;
            OptionElement y6 = y as OptionElement;

            if (x6 != null && y6 != null)
            {
                return(this.Equals(x6.InnerExpression, y6.InnerExpression));
            }
            LiteralElement x7 = x as LiteralElement;
            LiteralElement y7 = y as LiteralElement;

            if (x7 != null && y7 != null)
            {
                return(x7.Literal.Type == y7.Literal.Type && x7.InnerWord == y7.InnerWord);
            }
            if (x == null && y == null)
            {
                return(true);
            }
            if (x == null || y == null)
            {
                return(false);
            }
            return(false);
        }
Esempio n. 8
0
 /// <summary>
 /// expressions = selection ;
 /// </summary>
 /// <param name="selection">selection</param>
 protected override ExpressionsElement ReturnExpressions(SelectionElement selection)
 {
     return(new ExpressionsElement(selection));
 }