private void CreateAcceptAction(ParserState initialState, NonTerminal augmentedRoot)
 {
     var root = augmentedRoot.Productions[0].RValues[0];
     var shiftAction = initialState.Actions[root] as ShiftParserAction;
     var shiftOverRootState = shiftAction.NewState;
     shiftOverRootState.Actions[_grammar.Eof] = new AcceptParserAction();
 }
 public GrammarReductionConflict(ParserState state, TokenGrammarElement lookahead, ParserAction action1, ParserAction action2)
 {
     State = state;
     Lookahead = lookahead;
     Action1 = action1;
     Action2 = action2;
 }
Example #3
0
 public CParser(ParserState parserState, CLexer lexer)
 {
     this.ParserState = parserState;
     this.lexer = new LookAheadLexer(
         new CDirectiveLexer(parserState, lexer));
     this.grammar = new CGrammar();
 }
Example #4
0
  public XmlConverter(TextReader rdr, XmlWriter writer, IPlatform platform)
  {
      this.rdr = rdr;
      this.writer = writer;
      this.platform = platform;
      this.parserState = new ParserState();
 }
        public ParseResult Parse(Lexer lexer, ParserState initialState)
        {
            var context = new ParserContext(this, Automaton.Grammar, lexer);
            context.CurrentState = initialState;
            context.ParserStack.Push(new ParserNode(context, new GrammarDefinition("init"))
            {
                State = initialState
            });

            ReadNextToken(context);
            context.CurrentNode = new ParserNode(context, Automaton.Grammar.ToElement(lexer.Current.GetTokenCode()), lexer.Current);

            while (lexer.Current != null && context.Root == null && context.CurrentState != null)
            {
                // If no input and no default action (e.g. a grammar reduction),
                // that means we have to read the next token.
                if (context.CurrentNode == null && context.CurrentState.DefaultAction == null)
                    ReadNextToken(context);

                var action = GetNextAction(context);
                action.Execute(context);
            }

            lexer.SpecialBag.InsertNodesIntoAstNode(context.Root.Result);

            return ParseResult.FromContext(context);
        }
Example #6
0
        public CppFileParser(IAstPrinter printer, string file)
        {
            Rule parse_rule = grammar.file;

            string text = File.ReadAllText(file);
            printer.Clear();
            ParserState state = new ParserState(text);

            try
            {
                if (!parse_rule.Match(state))
                {
                    message = "Failed to parse file " + file;
                }
                else
                {
                    if (state.AtEndOfInput())
                    {
                        message = "Successfully parsed file";
                    }
                    else
                    {
                        message = "Failed to read end of input";
                    }
                }
                    
            }
            catch (ParsingException e)
            {
                state.ForceCompletion();
                message = e.Message;
            }

            printer.PrintNode(state.GetRoot(), 0);
        }
Example #7
0
        public void BuildIfStatement()
        {
            var grammer = new AndyStructuralGrammar();
            var p = new ParserState("if");

            Assert.That(grammer.if_keyword.Match(p), Is.True);

            p = new ParserState("if()");
            var rule = grammer.if_keyword + grammer.Delimiter("(") + grammer.Delimiter(")");
            Assert.That(rule.Match(p), Is.True);

            p = new ParserState("if (a == b)");
            Assert.That(grammer.if_condition.Match(p), Is.True);

            p = new ParserState("if(a == 10){a=20;}");
            var if_block = grammer.if_condition + grammer.block;
            Assert.That(if_block.Match(p), Is.True);

            p = new ParserState("if(a == 10){a=20;}");
            if_block = grammer.if_condition + new RecursiveRule(() => grammer.block);
            Assert.That(if_block.Match(p), Is.True);

            p = new ParserState("if(a == 10){a=20;}");
            Assert.That(grammer.control_statement.Match(p), Is.True);
        }
Example #8
0
        public bool Execute(ParserState state)
        {
            var builder = new StringBuilder();

            for (var i = 0; i < base.Children.Count; i++)
            {
                var action = base.Children[i];
                var subState = state.Clone();
                subState.CurrentItem = string.Empty;
                if (!action.Execute(subState))
                {
                    logger.LogDebug($"{state.Tracker} Http sub {i} action failed.");
                    return false;
                }
                else
                {
                    builder.Append(state.CurrentItem);
                }
            }

            var value = builder.ToString();
            logger.LogDebug($"{state.Tracker} Http setting {name} to {value}.");
            state.HTTPHeaders[name] = value;
            return true;
        }
Example #9
0
        public static ParserState Parse(string source, IAbstractSyntaxTreePrinter printer = null, Rule rule = null)
        {
            if (rule == null)
                rule = _grammar.file;

            var state = new ParserState(source);
            try
            {
                if (rule.Match(state))
                {
                    if (state.AtEndOfInput())
                    {
                        if (printer != null)
                            printer.Print(state.GetRoot());

                        return state;
                    }

                    throw new ParsingException(state, rule, message: "Failed to read end of input");
                }

                throw new ParsingException(state, rule, message: "Failed to parse source");
            }
            catch (ParsingException e)
            {
                return state.Exception(e);
            }
        }
Example #10
0
        private bool IncludeBinary(List<string> tokens, ParserState state)
        {
            if (tokens.Count == 1)
                throw new Exception(string.Format("No file specified for .incbin pragma", tokens[1]));

            tokens[1] = tokens[1].Replace("\"", string.Empty);

            byte[] data = GetBytesFromFile(state.WorkingDirectory + @"\" + tokens[1]);
            if (data == null)
                throw new Exception($"Error loading file '{tokens[1]}'");

            int begin = 0, length = data.Length;
            if (tokens.Count >= 3)
                if (!Int32.TryParse(tokens[2], out begin))
                    throw new Exception("Third paramter for incbin must be numeric");

            if (tokens.Count == 3)
            {
                length = length - begin;
            }

            if (tokens.Count == 4)
                if (!Int32.TryParse(tokens[3], out length))
                    throw new Exception("Fourth paramter for incbin must be numeric");

            if ((begin >= length) || (begin + length > data.Length))
                throw new Exception("Out of bounds for incbin");

            for (int i = 0; i < length; i++)
                state.Code.Add(data[i + begin]);
            return true;
        }
Example #11
0
    public ShiftParserAction(BnfTerm term, ParserState newState) {
      if (newState == null)
        throw new Exception("ParserShiftAction: newState may not be null. term: " + term.ToString());

      Term = term; 
      NewState = newState;
    }
Example #12
0
 public ActionRecord(ParserActionType actionType, ParserState newState, NonTerminal nonTerminal, int popCount)
 {
   ActionType = actionType;
   NewState = newState;
   NonTerminal = nonTerminal;
   PopCount = popCount;
 }
Example #13
0
 /// <summary>
 /// Indicates if the current parser state matches the rule.
 /// </summary>
 /// <param name="state">The state.</param>
 /// <returns></returns>
 public override bool Match(ParserState state)
 {
     while (this.FirstSon.Match(state))
     {
     }
     return true;
 }
 public PrecedenceBasedParserAction(BnfTerm shiftTerm, ParserState newShiftState, Production reduceProduction)
 {
     _reduceAction = new ReduceParserAction(reduceProduction);
     var reduceEntry = new ConditionalEntry(CheckMustReduce, _reduceAction, "(Precedence comparison)");
     ConditionalEntries.Add(reduceEntry);
     DefaultAction = _shiftAction = new ShiftParserAction(shiftTerm, newShiftState);
 }
Example #15
0
 protected override bool InternalMatch(ParserState state)
 {
     var old = state.Clone();
     bool result = Child.Match(state);
     state.Assign(old);
     return result;
 }
Example #16
0
 public List<Node> Parse(string input)
 {
     var state = new ParserState() { input = input, pos = 0 };
     if (!Match(state))
         throw new Exception(String.Format("Rule {0} failed to match", Name));
     return state.nodes;
 }
Example #17
0
        private static IParseResult ParseDocEscapedChar(ParserState state)
        {
            state.Advance();
              if (char.IsLetterOrDigit(state.Current))
            return FailedParse.Get;

              return new SuccessfulParse(state.Current.ToString());
        }
Example #18
0
 private void Reset()
 {
   _stack.Reset();
   _currentState = Data.InitialState;
   _currentLine = 0;
   _tokenCount = 0;
   _context.Errors.Clear();
 }
Example #19
0
 /// <summary>
 /// Indicates if the current parser state matches the rule.
 /// </summary>
 /// <param name="state">The state.</param>
 /// <returns></returns>
 public override bool Match(ParserState state)
 {
     if (this.Sons.Count == 0)
     {
         this.Sons.Add(ruleFunction());
     }
     return this.FirstSon.Match(state);
 }
Example #20
0
		private void InitBlock()
		{
			nodeTree = new ParserState();
			directives = null;
			jj_la1 = new int[53];
			jj_2_rtns = new Calls[12];
			jj_expentries = new ArrayList();
			jj_lasttokens = new int[100];
		}
Example #21
0
        private int ParseLabel(string line, ParserState state)
        {
            int colonPos = line.IndexOf(':');
            string labelName = line.Substring(0, colonPos);

            if (!state.Scopes.AddLabel(labelName.Trim().ToLowerInvariant(), state.Code.Count))
                throw new Exception($"Error adding label '{labelName}'");
            return colonPos + 1;
        }
Example #22
0
 /// <summary>
 /// Indicates if the current parser state matches the rule.
 /// </summary>
 /// <param name="state">The state.</param>
 /// <returns></returns>
 public override bool Match(ParserState state)
 {
     if (!state.CurrentInput.StartsWith(this.value))
     {
         return false;
     }
     state.CurrentPosition = state.CurrentPosition + this.value.Length;
     return true;
 }
Example #23
0
        public static bool TryParseComponents(string uri, UriKind kind, out UriElements elements, out string error)
        {
            uri = uri.Trim();

            ParserState state = new ParserState(uri, kind);
            elements = state.elements;
            error = null;

            if (uri.Length == 0 && (kind == UriKind.Relative || kind == UriKind.RelativeOrAbsolute))
            {
                state.elements.isAbsoluteUri = false;
                return true;
            }

            if (uri.Length <= 1 && kind == UriKind.Absolute)
            {
                error = "Absolute URI is too short";
                return false;
            }

            bool ok = ParseFilePath(state) &&
                ParseScheme(state);

            var scheme = state.elements.scheme;
            UriParser parser = null;
            if (!StringUtilities.IsNullOrEmpty(scheme))
            {
                parser = UriParser.GetParser(scheme);
                if (parser != null && !(parser is DefaultUriParser))
                    return true;
            }

            ok = ok &&
                ParseAuthority(state) &&
                ParsePath(state) &&
                ParseQuery(state) &&
                ParseFragment(state);

            if (StringUtilities.IsNullOrEmpty(state.elements.host) &&
                (scheme == Uri.UriSchemeHttp || scheme == Uri.UriSchemeGopher || scheme == Uri.UriSchemeNntp ||
                scheme == Uri.UriSchemeHttps || scheme == Uri.UriSchemeFtp))
                state.error = "Invalid URI: The Authority/Host could not be parsed.";

            if (!StringUtilities.IsNullOrEmpty(state.elements.host) &&
                Uri.CheckHostName(state.elements.host) == UriHostNameType.Unknown)
                state.error = "Invalid URI: The hostname could not be parsed.";

            if (!StringUtilities.IsNullOrEmpty(state.error))
            {
                elements = null;
                error = state.error;
                return false;
            }

            return true;
        }
Example #24
0
 public bool Execute(ParserState state)
 {
     logger.LogDebug($"{state.Tracker} ExtractOne has {base.Children.Count} child actions.");
     foreach(var item in base.Children)
     {
        if(item.Execute(state))
             return true;
     }
     return true;
 }
Example #25
0
        public bool Execute(ParserState state)
        {
            foreach (var action in base.Children)
            {
                if (!action.Execute(state))
                    return false;
            }

            return true;
        }
Example #26
0
        public void ParseMemoryAddress(string value, bool expected)
        {
            var grammer = new AsmBaseGrammar();
            var p = new ParserState(value);

            Assert.That(grammer.memoryAddress.Match(p), Is.EqualTo(expected));

            ParseNode node = p.GetRoot();
            Console.WriteLine(node.Value);
        }
Example #27
0
        public Assembler(string source)
            : this()
        {
            var parser = new ParserState(source);
            bool match = _grammar.program.Match(parser);
            if (match == false)
                throw new Exception("Cannot parse source file");

            _root = parser.GetRoot();
        }
Example #28
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Token"/> class.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="state">The state.</param>
 /// <exception cref="System.ArgumentNullException">Name of node cannot be null</exception>
 public Token(string name, ParserState state)
 {
     if (name == null)
     {
         throw new ArgumentNullException("Name of node cannot be null");
     }
     this.Name = name;
     this.Input = state.Input;
     this.Begin = state.CurrentPosition;
     this.Tokens = new List<Token>();
 }
Example #29
0
        public void ParseLine(string value, bool expected)
        {
            var grammer = new AsmStructuralGrammar();
            var p = new ParserState(value);

            Assert.That(grammer.line.Match(p), Is.EqualTo(expected));

            var printer = new CppStructuralOutputAsXml();
            printer.Print(p.GetRoot());
            Console.WriteLine(printer.AsXml());
        }
Example #30
0
        public void ParseOpCode(string value, bool expected)
        {
            var grammer = new AsmBaseGrammar();
            var printer = new CppStructuralOutputAsXml();
            var p = new ParserState(value);

            Assert.That(grammer.opcode.Match(p), Is.EqualTo(expected));

            ParseNode node = p.GetRoot();
            Console.WriteLine(node.Value);
        }
Example #31
0
 public ParserEventArgs(ParserState state)
 {
     _state = state;
 }
Example #32
0
        /// <summary>
        /// Parse the productions:
        /// implicit_document    ::= block_node DOCUMENT-END*
        ///                          *
        /// explicit_document    ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
        ///                          *************************
        /// </summary>
        private ParsingEvent ParseDocumentStart(bool isImplicit)
        {
            if (currentToken is VersionDirective)
            {
                // EB22
                throw new SyntaxErrorException("While parsing a document start node, could not find document end marker before version directive.");
            }

            // Parse extra document end indicators.

            var current = GetCurrentToken();

            if (!isImplicit)
            {
                while (current is DocumentEnd)
                {
                    Skip();
                    current = GetCurrentToken();
                }
            }

            if (current == null)
            {
                throw new SyntaxErrorException("Reached the end of the stream while parsing a document start.");
            }

            if (current is Scalar && (state == ParserState.ImplicitDocumentStart || state == ParserState.DocumentStart))
            {
                isImplicit = true;
            }

            // Parse an isImplicit document.

            if (isImplicit && !(current is VersionDirective || current is TagDirective || current is DocumentStart || current is StreamEnd || current is DocumentEnd) || current is BlockMappingStart)
            {
                var directives = new TagDirectiveCollection();
                ProcessDirectives(directives);

                states.Push(ParserState.DocumentEnd);

                state = ParserState.BlockNode;

                return(new Events.DocumentStart(null, directives, true, current.Start, current.End));
            }

            // Parse an explicit document.

            else if (!(current is StreamEnd || current is DocumentEnd))
            {
                var start            = current.Start;
                var directives       = new TagDirectiveCollection();
                var versionDirective = ProcessDirectives(directives);

                current = GetCurrentToken() ?? throw new SemanticErrorException("Reached the end of the stream while parsing a document start");

                if (!(current is DocumentStart))
                {
                    throw new SemanticErrorException(current.Start, current.End, "Did not find expected <document start>.");
                }

                states.Push(ParserState.DocumentEnd);

                state = ParserState.DocumentContent;

                var end = current.End;
                Skip();
                return(new Events.DocumentStart(versionDirective, directives, false, start, end));
            }

            // Parse the stream end.

            else
            {
                if (current is DocumentEnd)
                {
                    Skip();
                }
                state = ParserState.StreamEnd;

                current = GetCurrentToken() ?? throw new SemanticErrorException("Reached the end of the stream while parsing a document start");

                var evt = new Events.StreamEnd(current.Start, current.End);
                // Do not call skip here because that would throw an exception
                if (scanner.MoveNextWithoutConsuming())
                {
                    throw new InvalidOperationException("The scanner should contain no more tokens.");
                }
                return(evt);
            }
        }
Example #33
0
        public bool Parse()
        {
            if (isOK)
            {
                return(true);
            }

            bool finish = false;

            while (!finish)
            {
                switch (state)
                {
                case ParserState.PacketSize:
                    if (buffer.Length < packetSizeLength)
                    {
                        finish = true;
                    }
                    else
                    {
                        buffer.Read(memoryStream.GetBuffer(), 0, packetSizeLength);

                        switch (packetSizeLength)
                        {
                        case Packet.PacketSizeLength4:
                            packetSize = BitConverter.ToInt32(memoryStream.GetBuffer(), 0);
                            if (packetSize > ushort.MaxValue * 16 || packetSize < Packet.MinPacketSize)
                            {
                                throw new Exception($"recv packet size error, 可能是外网探测端口: {packetSize}");
                            }
                            break;

                        case Packet.PacketSizeLength2:
                            packetSize = BitConverter.ToUInt16(memoryStream.GetBuffer(), 0);
                            if (packetSize > ushort.MaxValue || packetSize < Packet.MinPacketSize)
                            {
                                throw new Exception($"recv packet size error:, 可能是外网探测端口: {packetSize}");
                            }
                            break;

                        default:
                            throw new Exception("packet size byte count must be 2 or 4!");
                        }
                        state = ParserState.PacketBody;
                    }
                    break;

                case ParserState.PacketBody:
                    if (buffer.Length < packetSize)
                    {
                        finish = true;
                    }
                    else
                    {
                        memoryStream.Seek(0, SeekOrigin.Begin);
                        memoryStream.SetLength(packetSize);
                        byte[] bytes = memoryStream.GetBuffer();
                        buffer.Read(bytes, 0, packetSize);
                        isOK   = true;
                        state  = ParserState.PacketSize;
                        finish = true;
                    }
                    break;
                }
            }
            return(isOK);
        }