Пример #1
0
 public override void ExitParseRule([NotNull] BinShredParser.ParseRuleContext context)
 {
     processingActions.Pop();
 }
Пример #2
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);
                        }
                                            ));
                    }
                }
            }
        }