Beispiel #1
0
 void VisitTokens(Reduction reduction)
 {
     foreach (Token token in reduction.Tokens)
     {
         token.Visit(this);
     }
 }
Beispiel #2
0
 public void OnReduction(Reduction reduction)
 {
     foreach (Token token in reduction.Tokens)
     {
         token.Visit(this);
     }
 }
Beispiel #3
0
 public void OnReduction(Reduction reduction)
 {
     if (m_isSynthetic(reduction))
         VisitTokens(reduction);
     else
     {
         Assert(m_reductionHandler.ContainsKey(reduction.SymbolName));
         m_reductionHandler[reduction.SymbolName](reduction);
     }
 }
Beispiel #4
0
        public static UIGroup FromReduction(GOLDEngine.Reduction r, Dictionary <Token, object> dataMap)
        {
            UIGroup result = new UIGroup();
            // <Object> ::= BeginLiteral <Content> EndLiteral
            var content     = (List <UINode>)dataMap[r[1]];
            var sharedProps = content.FirstOrDefault(x => x.Name == "SetSharedProperties");

            result.SharedProperties = sharedProps;
            result.Children         = content.Where(x => x != sharedProps).ToList();

            return(result);
        }
Beispiel #5
0
 public void OnReduction(Reduction reduction)
 {
     if (m_isSynthetic(reduction))
     {
         VisitTokens(reduction);
     }
     else
     {
         Assert(m_reductionHandler.ContainsKey(reduction.SymbolName));
         m_reductionHandler[reduction.SymbolName](reduction);
     }
 }
Beispiel #6
0
        protected List <Terminal> ExpectTerminalList(Reduction reduction)
        {
            List <Terminal>      found          = new List <Terminal>();
            Predicate <Terminal> expectTerminal = terminal =>
            {
                found.Add(terminal);
                return(true);
            };
            TokenParser parser = new TokenParser(m_isNoise, m_isSynthetic);

            parser.OnCreate(expectTerminal, reduction);
            Assert(found.Count > 0);
            return(found);
        }
Beispiel #7
0
        protected Terminal ExpectOneTerminal(Reduction reduction)
        {
            Terminal             found          = null;
            Predicate <Terminal> expectTerminal = terminal =>
            {
                if (found != null)
                {
                    return(false);
                }
                found = terminal;
                return(true);
            };
            TokenParser parser = new TokenParser(m_isNoise, m_isSynthetic);

            parser.OnCreate(expectTerminal, reduction);
            Assert(found != null);
            return(found);
        }
Beispiel #8
0
        public void OnReduction(Reduction reduction)
        {
            TokenStart(reduction);
            int nTokens = reduction.Tokens.Length;

            switch (nTokens)
            {
            case 0:
                m_stringBuilder.Append("[]");
                break;

            //case 1:
            //    m_stringBuilder.Append("[ ");
            //    reduction.Tokens[0].Visit(this);
            //    m_stringBuilder.Append(" ]");
            //    break;
            default:
                m_stringBuilder.AppendLine("[");
                ++m_nesting;
                for (int i = 0; i < nTokens; ++i)
                {
                    Token token = reduction.Tokens[i];
                    //m_stringBuilder.Append(' ', m_nesting * 4);
                    token.Visit(this);
                    if (i != nTokens - 1)
                    {
                        m_stringBuilder.AppendLine(",");
                    }
                }
                --m_nesting;
                m_stringBuilder.AppendLine();
                AppendIndent();
                m_stringBuilder.Append("]");
                break;
            }
            TokenEnd();
        }
Beispiel #9
0
        internal ParseResult ParseLALR(Token NextToken)
        {
            //This function analyzes a token and either:
            //  1. Makes a SINGLE reduction and pushes a complete Reduction object on the m_Stack
            //  2. Accepts the token and shifts
            //  3. Errors and places the expected symbol indexes in the Tokens list
            //The Token is assumed to be valid and WILL be checked
            //If an action is performed that requires controlt to be returned to the user, the function returns true.
            //The Message parameter is then set to the type of action.

            LRAction ParseAction = m_loaded.FindLRAction(m_CurrentLALR, NextToken.Symbol);

            // Work - shift or reduce
            if ((ParseAction != null))
            {
                //'Debug.WriteLine("Action: " & ParseAction.Text)

                switch (ParseAction.Type)
                {
                case LRActionType.Accept:
                    return(ParseResult.Accept);

                case LRActionType.Shift:
                    Push(NextToken, ParseAction.Value);
                    return(ParseResult.Shift);

                case LRActionType.Reduce:
                    //Produce a reduction - remove as many tokens as members in the rule & push a nonterminal token
                    Production Prod = m_loaded.GetProduction(ParseAction);

                    ParseResult Result;
                    Token       Head;
                    //======== Create Reduction
                    if (m_TrimReductions & Prod.ContainsOneNonTerminal())
                    {
                        //The current rule only consists of a single nonterminal and can be trimmed from the
                        //parse tree. Usually we create a new Reduction, assign it to the Data property
                        //of Head and push it on the m_Stack. However, in this case, the Data property of the
                        //Head will be assigned the Data property of the reduced token (i.e. the only one
                        //on the m_Stack).
                        //In this case, to save code, the value popped of the m_Stack is changed into the head.

                        //Build a Reduction
                        Head = m_Stack.Pop().Token;
                        Head.TrimReduction(Prod.Head());

                        Result = ParseResult.ReduceEliminated;
                    }
                    else
                    {
                        int          nTokens = Prod.Handle().Count;
                        List <Token> tokens  = new List <Token>(nTokens);
                        for (int i = Prod.Handle().Count - 1; i >= 0; --i)
                        {
                            TokenState popped = Pop();
                            while (popped.IsExtra)
                            {
                                tokens.Insert(0, popped.Token);
                                popped = Pop();
                            }
                            tokens.Insert(0, popped.Token);
                        }
                        //Say that the reduction's Position is the Position of its first child.
                        Head   = new Reduction(Prod, tokens.ToArray());
                        Result = ParseResult.ReduceNormal;
                    }

                    //========== Goto
                    short GotoState = m_Stack.Peek().GotoState;

                    LRAction found = m_loaded.FindLRAction(GotoState, Prod.Head());
                    if ((found != null) && (found.Type == LRActionType.Goto))
                    {
                        Push(Head, found.Value);
                        return(Result);
                    }
                    else
                    {
                        //========= If action not found here, then we have an Internal Table Error!!!!
                        return(ParseResult.InternalError);
                    }

                default:
                    return(ParseResult.InternalError);
                }
            }
            else
            {
                return(ParseResult.SyntaxError);
            }
        }
Beispiel #10
0
 public void OnReduction(Reduction reduction)
 {
     foreach (Token token in reduction.Tokens)
     {
         token.Visit(this);
     }
 }
Beispiel #11
0
 public void OnReduction(Reduction reduction)
 {
     TokenStart(reduction);
     int nTokens = reduction.Tokens.Length;
     switch (nTokens)
     {
         case 0:
             m_stringBuilder.Append("[]");
             break;
         //case 1:
         //    m_stringBuilder.Append("[ ");
         //    reduction.Tokens[0].Visit(this);
         //    m_stringBuilder.Append(" ]");
         //    break;
         default:
             m_stringBuilder.AppendLine("[");
             ++m_nesting;
             for (int i = 0; i < nTokens; ++i)
             {
                 Token token = reduction.Tokens[i];
                 //m_stringBuilder.Append(' ', m_nesting * 4);
                 token.Visit(this);
                 if (i != nTokens - 1)
                 {
                     m_stringBuilder.AppendLine(",");
                 }
             }
             --m_nesting;
             m_stringBuilder.AppendLine();
             AppendIndent();
             m_stringBuilder.Append("]");
             break;
     }
     TokenEnd();
 }
Beispiel #12
0
 void VisitTokens(Reduction reduction)
 {
     foreach (Token token in reduction.Tokens)
     {
         token.Visit(this);
     }
 }
Beispiel #13
0
 // Call this method from your HandleTokens implementation.
 protected void RecursiveList(Reduction reduction)
 {
     VisitTokens(reduction);
 }
Beispiel #14
0
 protected void OnCreate(Predicate<Terminal> expectTerminal, Reduction reduction)
 {
     ExpectTerminal = expectTerminal;
     VisitTokens(reduction);
 }
Beispiel #15
0
        public static UISharedProperties FromReduction(GOLDEngine.Reduction r)
        {
            UISharedProperties result = new UISharedProperties();

            return(result);
        }
Beispiel #16
0
 protected Terminal ExpectOneTerminal(Reduction reduction)
 {
     Terminal found = null;
     Predicate<Terminal> expectTerminal = terminal =>
     {
         if (found != null)
             return false;
         found = terminal;
         return true;
     };
     TokenParser parser = new TokenParser(m_isNoise, m_isSynthetic);
     parser.OnCreate(expectTerminal, reduction);
     Assert(found != null);
     return found;
 }
Beispiel #17
0
 // Call this method from your HandleTokens implementation.
 protected void RecursiveList(Reduction reduction)
 {
     VisitTokens(reduction);
 }
Beispiel #18
0
        internal ParseResult ParseLALR(Token NextToken)
        {
            //This function analyzes a token and either:
            //  1. Makes a SINGLE reduction and pushes a complete Reduction object on the m_Stack
            //  2. Accepts the token and shifts
            //  3. Errors and places the expected symbol indexes in the Tokens list
            //The Token is assumed to be valid and WILL be checked
            //If an action is performed that requires controlt to be returned to the user, the function returns true.
            //The Message parameter is then set to the type of action.

            LRAction ParseAction = m_loaded.FindLRAction(m_CurrentLALR, NextToken.Symbol);

            // Work - shift or reduce
            if ((ParseAction != null))
            {
                //'Debug.WriteLine("Action: " & ParseAction.Text)

                switch (ParseAction.Type)
                {
                    case LRActionType.Accept:
                        return ParseResult.Accept;

                    case LRActionType.Shift:
                        Push(NextToken, ParseAction.Value);
                        return ParseResult.Shift;

                    case LRActionType.Reduce:
                        //Produce a reduction - remove as many tokens as members in the rule & push a nonterminal token
                        Production Prod = m_loaded.GetProduction(ParseAction);

                        ParseResult Result;
                        Token Head;
                        //======== Create Reduction
                        if (m_TrimReductions & Prod.ContainsOneNonTerminal())
                        {
                            //The current rule only consists of a single nonterminal and can be trimmed from the
                            //parse tree. Usually we create a new Reduction, assign it to the Data property
                            //of Head and push it on the m_Stack. However, in this case, the Data property of the
                            //Head will be assigned the Data property of the reduced token (i.e. the only one
                            //on the m_Stack).
                            //In this case, to save code, the value popped of the m_Stack is changed into the head.

                            //Build a Reduction
                            Head = m_Stack.Pop().Token;
                            Head.TrimReduction(Prod.Head());

                            Result = ParseResult.ReduceEliminated;
                        }
                        else
                        {
                            int nTokens = Prod.Handle().Count;
                            List<Token> tokens = new List<Token>(nTokens);
                            for (int i = Prod.Handle().Count - 1; i >= 0; --i)
                            {
                                TokenState popped = Pop();
                                while (popped.IsExtra)
                                {
                                    tokens.Insert(0, popped.Token);
                                    popped = Pop();
                                }
                                tokens.Insert(0, popped.Token);
                            }
                            //Say that the reduction's Position is the Position of its first child.
                            Head = new Reduction(Prod, tokens.ToArray());
                            Result = ParseResult.ReduceNormal;
                        }

                        //========== Goto
                        short GotoState = m_Stack.Peek().GotoState;

                        LRAction found = m_loaded.FindLRAction(GotoState, Prod.Head());
                        if ((found != null) && (found.Type == LRActionType.Goto))
                        {
                            Push(Head, found.Value);
                            return Result;
                        }
                        else
                        {
                            //========= If action not found here, then we have an Internal Table Error!!!!
                            return ParseResult.InternalError;
                        }

                    default:
                        return ParseResult.InternalError;
                }

            }
            else
            {
                return ParseResult.SyntaxError;
            }
        }
Beispiel #19
0
 protected List<Terminal> ExpectTerminalList(Reduction reduction)
 {
     List<Terminal> found = new List<Terminal>();
     Predicate<Terminal> expectTerminal = terminal =>
     {
         found.Add(terminal);
         return true;
     };
     TokenParser parser = new TokenParser(m_isNoise, m_isSynthetic);
     parser.OnCreate(expectTerminal, reduction);
     Assert(found.Count > 0);
     return found;
 }
Beispiel #20
0
        private object CreateNewObject(GOLDEngine.Reduction r)
        {
            object result = r.ToText();

            for (int i = 0; i < r.Count; i++)
            {
                if (!DataMap.ContainsKey(r[i]))
                {
                    DataMap[r[i]] = r[i].ToText();
                }
            }

            switch ((ProductionIndex)r.Production.TableIndex())
            {
            case ProductionIndex.Objects:
                var newResult = new List <UINode>();
                newResult.Add((UINode)DataMap[r[0]]);
                newResult.AddRange((List <UINode>)DataMap[r[1]]);
                result  = newResult;
                program = result;
                // <Objects> ::= <Object> <Objects>
                break;

            case ProductionIndex.Objects2:
                // <Objects> ::= <Object>
                var newResult2 = new List <UINode>();
                newResult2.Add((UINode)DataMap[r[0]]);
                result  = newResult2;
                program = result;
                break;

            case ProductionIndex.Object_Beginliteral_Endliteral:
                // <Object> ::= BeginLiteral <Content> EndLiteral
                result = UIGroup.FromReduction(r, DataMap);
                break;

            case ProductionIndex.Object:
                // <Object> ::= <Start Tag>
                result = DataMap[r[0]];
                break;

            case ProductionIndex.Object2:
                // <Object> ::= <Unary Tag>
                break;

            case ProductionIndex.Starttag_Lt_Id_Gt:
                // <Start Tag> ::= '<' ID <OptionalID> <Attributes> '>'
                var node = new UINode();
                node.Name = (string)DataMap[r[1]];
                node.ID   = (string)DataMap[r[2]];
                var atts = (List <KeyValuePair <string, string> >)DataMap[r[3]];
                foreach (var att in atts)
                {
                    node[att.Key] = att.Value;
                }
                result = node;
                break;

            case ProductionIndex.Endtag_Ltdiv_Id_Gt:
                // <End Tag> ::= '</' ID '>'
                break;

            case ProductionIndex.Unarytag_Lt_Id_Divgt:
                // <Unary Tag> ::= '<' ID <OptionalID> <Attributes> '/>'
                break;

            case ProductionIndex.Optionalid_Stringliteral:
                // <OptionalID> ::= StringLiteral
                result = GetStringLiteral((string)DataMap[r[0]]);
                break;

            case ProductionIndex.Optionalid:
                // <OptionalID> ::=
                result = null;
                break;

            case ProductionIndex.Content:
                // <Content> ::= <Objects>
                var newResult3 = new List <UINode>();
                newResult3.AddRange((List <UINode>)DataMap[r[0]]);
                result = newResult3;
                break;

            case ProductionIndex.Content2:
                // <Content> ::= <Text>
                var newResult4 = new List <UINode>();
                newResult4.Add((UINode)DataMap[r[0]]);
                result = newResult4;
                break;

            case ProductionIndex.Attributes:
                // <Attributes> ::= <Attribute> <Attributes>
                var attributeList = new List <KeyValuePair <string, string> >();
                attributeList.Add((KeyValuePair <string, string>)DataMap[r[0]]);
                if (r.Count > 1)
                {
                    attributeList.AddRange((List <KeyValuePair <string, string> >)DataMap[r[1]]);
                }
                result = attributeList;
                break;

            case ProductionIndex.Attributes2:
                // <Attributes> ::=
                result = new List <KeyValuePair <string, string> >();
                break;

            case ProductionIndex.Attribute_Id_Eq_Stringliteral:
                // <Attribute> ::= ID '=' StringLiteral
                result = new KeyValuePair <string, string>((string)DataMap[r[0]], GetStringLiteral((string)DataMap[r[2]]));
                break;

            case ProductionIndex.Attribute_Id_Eq_Id:
                // <Attribute> ::= ID '=' ID
                result = new KeyValuePair <string, string>((string)DataMap[r[0]], (string)DataMap[r[2]]);
                break;

            case ProductionIndex.Text:
                // <Text> ::= <Text> <Word>
                break;

            case ProductionIndex.Text2:
                // <Text> ::= <Word>
                break;

            case ProductionIndex.Word_Id:
                // <Word> ::= ID
                break;

            case ProductionIndex.Word_Eq:
                // <Word> ::= '='
                break;

            case ProductionIndex.Word_Charname:
                // <Word> ::= CharName
                break;

            case ProductionIndex.Word_Charnumber:
                // <Word> ::= CharNumber
                break;
            }  //switch

            DataMap[r] = result;
            return(result);
        }
Beispiel #21
0
 protected void OnCreate(Predicate <Terminal> expectTerminal, Reduction reduction)
 {
     ExpectTerminal = expectTerminal;
     VisitTokens(reduction);
 }