Exemple #1
0
        public override void Init(Irony.Ast.AstContext context, Irony.Parsing.ParseTreeNode treeNode)
        {
            hijos = new List <Arbol>();//inicializamos la lista
            base.Init(context, treeNode);
            switch ((String)treeNode.Term.Name)
            {           //  0 1 2
            case "E":   // E + E | E - E | E * E  | num
                if (treeNode.ChildNodes.Count > 1)
                {
                    Name = treeNode.ChildNodes[1].FindTokenAndGetText();  //SIGNO
                    hijos.Add((Arbol)treeNode.ChildNodes[0].AstNode);     // PRIMER VALOR
                    hijos.Add((Arbol)treeNode.ChildNodes[2].AstNode);     // SEGUNDO VALOR
                }
                else
                {
                    Name = treeNode.ChildNodes[0].FindTokenAndGetText();     //OBTENEMOS EL NUMERO
                }
                break;

            case "number":
                Name = treeNode.Token.Text;
                break;

            default:
                break;
            }
        }
Exemple #2
0
 public override void Init(Irony.Parsing.ParsingContext context, Irony.Parsing.ParseTreeNode treeNode)
 {
     base.Init(context, treeNode);
     if (treeNode.Token.ValueString.Length > MaxLen)
     {
         context.AddParserMessage(ParserErrorLevel.Error, this.Span, "The maximum length is {0} character{1}.", MaxLen, MaxLen != 1 ? "s" : string.Empty);
     }
 }
Exemple #3
0
        public override void Init(Irony.Ast.AstContext context, Irony.Parsing.ParseTreeNode treeNode)
        {
            base.Init(context, treeNode);

            Identifier = treeNode.ChildNodes[0].AstNode as IdentifierNode;
            if (treeNode.ChildNodes.Count > 1)
            {
                Source = (SourceNode)treeNode.ChildNodes[1].AstNode;
            }
        }
Exemple #4
0
        public override void Init(Irony.Parsing.ParsingContext context, Irony.Parsing.ParseTreeNode treeNode)
        {
            base.Init(context, treeNode);

            DateTime date;
            var      cleanDate = GetCleanDate(treeNode);

            if (!TryParseDate(cleanDate, out date))
            {
                var message = string.Format("{0} is not a valid date", GetCleanDate(treeNode));
                throw new AstException(this, message);
            }

            Value    = date;
            AsString = cleanDate;
        }
 public override void Init(AstContext context, Irony.Parsing.ParseTreeNode treeNode)
 {
     base.Init(context, treeNode);
     Characters = treeNode.ChildNodes.Select(cn => cn.AstNode as CharacterDeclarationNode).ToList();
 }
Exemple #6
0
 public override void Init(Irony.Ast.AstContext context, Irony.Parsing.ParseTreeNode treeNode)
 {
     base.Init(context, treeNode);
     Value = Value.str2varname();
 }
Exemple #7
0
 public virtual string GetParseNodeCaption(ParseTreeNode node)
 {
     return(_grammarData != null?_grammarData.Grammar.GetParseNodeCaption(node) : Name);
 }
Exemple #8
0
 //Gives a chance of custom AST node creation at Grammar level
 // by default calls Term's method
 public virtual void CreateAstNode(ParsingContext context, ParseTreeNode nodeInfo) {
   nodeInfo.Term.CreateAstNode(context, nodeInfo);
 }
        private bool TryRecoverImpl()
        {
            //1. We need to find a state in the stack that has a shift item based on error production (with error token),
            // and error terminal is current. This state would have a shift action on error token.
            ParserAction nextAction = FindErrorShiftActionInStackTemp();

            if (nextAction == null)
            {
                return(false);
            }

            var firstBnfTerm = nextAction.NewState.Actions.Keys.FirstOrDefault();

            Context.AddTrace(Resources.MsgTraceRecoverReducing);
            Context.AddTrace(Resources.MsgTraceRecoverAction, nextAction);

            // Inject faked node
            var newLineNode = new ParseTreeNode(firstBnfTerm);

            Context.ParserInputStack.Insert(0, newLineNode);
            var saveParserInput = Context.CurrentParserInput;

            Context.CurrentParserInput = newLineNode;

            nextAction = FindActionForStateAndInput();

            while (nextAction != null && Context.CurrentParserInput != null)
            {
                switch (nextAction.ActionType)
                {
                case ParserActionType.Shift:
                    ExecuteShift(nextAction);
                    break;

                case ParserActionType.Operator:
                    ExecuteOperatorAction(nextAction);
                    break;

                case ParserActionType.Reduce:
                    ExecuteReduce(nextAction);
                    break;

                case ParserActionType.Code:
                    ExecuteConflictAction(nextAction);
                    break;

                case ParserActionType.Accept:
                    ExecuteAccept(nextAction);
                    break;
                }
                nextAction = FindActionForStateAndInput();
            }

            Context.ParserInputStack.RemoveAt(0);
            Context.CurrentParserInput = saveParserInput;

            if (!Context.CurrentParserState.Actions.TryGetValue(Context.CurrentParserInput.Term, out nextAction))
            {
                Context.ParserInputStack.Clear();
                Context.CurrentParserInput = null;
            }

            return(true);
            //ExecuteShiftTemp(firstBnfTerm, nextAction);

/*
 *      var action = GetReduceActionInCurrentState();
 *      if (action != null)
 *      {
 *          //Clear all input token queues and buffered input, reset location back to input position token queues;
 *          //Reduce error production - it creates parent non-terminal that "hides" error inside
 *          ExecuteReduce(action);
 *          return true; //we recovered
 *      }
 *      return true;
 *
 *    ParserAction errorShiftAction = FindErrorShiftActionInStack();
 *    if (errorShiftAction == null) return false; //we failed to recover
 *    Context.AddTrace(Resources.MsgTraceRecoverFoundState, Context.CurrentParserState);
 *    //2. Shift error token - execute shift action
 *    Context.AddTrace(Resources.MsgTraceRecoverShiftError, errorShiftAction);
 *    ExecuteShift(errorShiftAction);
 *    //4. Now we need to go along error production until the end, shifting tokens that CAN be shifted and ignoring others.
 *    //   We shift until we can reduce
 *    Context.AddTrace(Resources.MsgTraceRecoverShiftTillEnd);
 *    while (true) {
 *      if (Context.CurrentParserInput == null)
 *        ReadInput();
 *      if (Context.CurrentParserInput.Term == _grammar.Eof)
 *        return false;
 *      //Check if we can reduce
 *      action = GetReduceActionInCurrentState();
 *      if (action != null) {
 *        //Clear all input token queues and buffered input, reset location back to input position token queues;
 *        Context.SetSourceLocation(Context.CurrentParserInput.Span.Location);
 *        Context.ParserInputStack.Clear();
 *        Context.CurrentParserInput = null;
 *
 *        //Reduce error production - it creates parent non-terminal that "hides" error inside
 *        Context.AddTrace(Resources.MsgTraceRecoverReducing);
 *        Context.AddTrace(Resources.MsgTraceRecoverAction, action);
 *        ExecuteReduceOnError(action);
 *        return true; //we recovered
 *      }
 *      //No reduce action in current state. Try to shift current token or throw it away or reduce
 *      action = GetShiftActionInCurrentState();
 *      if (action != null)
 *        ExecuteShift(action); //shift input token
 *      else //simply read input
 *        ReadInput();
 *    }
 */
        }//method