Exemple #1
0
        private void ProcessByteExtraction(string regionName, string ruleLabel, string ruleComment, ByteOptionContext byteOptions, int bytes, string byteLabel)
        {
            // See if it has a "described by" annotation, which we'll use
            // as a lookup table to augment the comment.
            string describedBy = null;

            if (byteOptions.label() != null)
            {
                describedBy = byteOptions.label().GetText();
            }

            // Process the bytes based on the byte format (i.e.: "as ASCII")
            ByteFormatContext byteFormat = byteOptions.byteFormat();

            if (byteFormat != null)
            {
                ProcessByByteFormat(byteFormat, (int)bytes, byteLabel, regionName, ruleLabel, describedBy, ruleComment);
            }
            else
            {
                // This has no byte format. It is just bytes.
                this.parseActions[regionName].Add(
                    new ParseAction((content, contentPosition) =>
                {
                    if (!String.IsNullOrEmpty(byteLabel))
                    {
                        bytes = GetByteCountFromLabel(byteLabel);
                    }

                    byte[] element = new byte[bytes];
                    Array.Copy(content, (int)contentPosition, element, 0, bytes);

                    ParseElement(ruleLabel, element, describedBy, ruleComment);
                    return((int)bytes);
                }
                                    ));
            }
        }
	public ByteOptionContext byteOption() {
		ByteOptionContext _localctx = new ByteOptionContext(Context, State);
		EnterRule(_localctx, 14, RULE_byteOption);
		int _la;
		try {
			EnterOuterAlt(_localctx, 1);
			{
			State = 88; sizeReference();
			State = 89; Match(BYTES);
			State = 97;
			_la = TokenStream.La(1);
			if (_la==AS) {
				{
				State = 90; Match(AS);
				State = 91; byteFormat();
				State = 95;
				_la = TokenStream.La(1);
				if (_la==DESCRIBED) {
					{
					State = 92; Match(DESCRIBED);
					State = 93; Match(BY);
					State = 94; label();
					}
				}

				}
			}

			}
		}
		catch (RecognitionException re) {
			_localctx.exception = re;
			ErrorHandler.ReportError(this, re);
			ErrorHandler.Recover(this, re);
		}
		finally {
			ExitRule();
		}
		return _localctx;
	}
Exemple #3
0
        public override void EnterParseRule([NotNull] BinShredParser.ParseRuleContext context)
        {
            string regionName = context.label().GetText();

            parseActions[regionName] = new List <ParseAction>();
            processingActions.Push(regionName);

            if (String.IsNullOrEmpty(startAction))
            {
                startAction = regionName;
            }

            foreach (RuleBodyContext body in context.ruleBody())
            {
                // This is a rule that describes additional actions to be taken from something
                // already parsed
                // "(additional properties identified by propertyName from lookupTableName)"
                if (body.ADDITIONAL() != null)
                {
                    string propertyName    = body.propertyName().GetText();
                    string lookupTableName = body.lookupTableName().GetText();

                    ProcessAdditionalProperties(propertyName, lookupTableName, regionName);
                    continue;
                }

                // Get the rule's label and descriptive comment (/** */)
                string ruleLabel   = body.label().GetText();
                string ruleComment = null;

                ITerminalNode docComment = body.DOC_COMMENT();
                if (docComment != null)
                {
                    ruleComment = docComment.GetText();
                    ruleComment = ruleComment.Substring(3, ruleComment.Length - 5).Trim();
                }

                // Get the rule's options
                RuleOptionsContext options = body.ruleOptions();
                if (options != null)
                {
                    // This is a rule with actual options (i.e.: "(4 bytes as int32)")
                    ByteOptionContext byteOptions = options.byteOption();

                    int    bytes     = 0;
                    String byteLabel = String.Empty;

                    if (byteOptions.sizeReference().INT() != null)
                    {
                        bytes = Int32.Parse(byteOptions.sizeReference().INT().GetText());
                    }
                    else
                    {
                        byteLabel = byteOptions.sizeReference().label().GetText();
                    }

                    // This is a rule that extracts bytes in some format
                    ProcessByteExtraction(regionName, ruleLabel, ruleComment, byteOptions, bytes, byteLabel);
                }
                else
                {
                    // This is a rule with a reference to another rule
                    if (body.ITEMS() != null)
                    {
                        // This is a rule which creates several records of a given record type
                        // (i.e.: (12 items) or (byteCount items)

                        int    items     = 0;
                        string itemLabel = String.Empty;

                        if (body.sizeReference().INT() != null)
                        {
                            items = Int32.Parse(body.sizeReference().INT().GetText());
                        }
                        else
                        {
                            itemLabel = body.sizeReference().label().GetText();
                        }

                        ProcessCountedRule(regionName, ruleLabel, ruleComment, items, itemLabel);
                    }
                    else
                    {
                        this.parseActions[regionName].Add(
                            new ParseAction((content, contentPosition) =>
                        {
                            OrderedDictionary currentResult = new OrderedDictionary(StringComparer.OrdinalIgnoreCase);
                            results.Peek().Add(ruleLabel, currentResult);

                            RunRule(ruleComment, ruleLabel, currentResult);
                            return(0);
                        }
                                            ));
                    }
                }
            }
        }