Class: ParseTreeMatch

Inherits:
Object show all
Defined in:
lib/antlr4/tree/ParseTreeMatch.rb

Overview

Represents the result of matching a ParseTree against a tree pattern.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, pattern, labels, mismatchedNode) ⇒ ParseTreeMatch

Returns a new instance of ParseTreeMatch.

Raises:

  • (Exception)


19
20
21
22
23
24
25
26
27
# File 'lib/antlr4/tree/ParseTreeMatch.rb', line 19

def initialize(tree, pattern, labels, mismatchedNode)
    raise Exception.new("tree cannot be null") if tree.nil? 
    raise Exception.new("pattern cannot be null") if pattern.nil?
    raise Exception.new("labels cannot be null") if labels.nil? 
    self.tree = tree
    self.pattern = pattern
    self.labels = labels
    self.mismatchedNode = mismatchedNode
end

Instance Attribute Details

#labelsObject

Constructs a new instance of ParseTreeMatch from the specified parse tree and pattern.

ParseTree objects located by the tree pattern matching process. pattern during the matching process.

Parameters:

  • tree

    The parse tree to match against the pattern.

  • pattern

    The parse tree pattern.

  • labels

    A mapping from label names to collections of

  • mismatchedNode

    The first node which failed to match the tree



18
19
20
# File 'lib/antlr4/tree/ParseTreeMatch.rb', line 18

def labels
  @labels
end

#mismatchedNodeObject

Constructs a new instance of ParseTreeMatch from the specified parse tree and pattern.

ParseTree objects located by the tree pattern matching process. pattern during the matching process.

Parameters:

  • tree

    The parse tree to match against the pattern.

  • pattern

    The parse tree pattern.

  • labels

    A mapping from label names to collections of

  • mismatchedNode

    The first node which failed to match the tree



18
19
20
# File 'lib/antlr4/tree/ParseTreeMatch.rb', line 18

def mismatchedNode
  @mismatchedNode
end

#patternObject

Constructs a new instance of ParseTreeMatch from the specified parse tree and pattern.

ParseTree objects located by the tree pattern matching process. pattern during the matching process.

Parameters:

  • tree

    The parse tree to match against the pattern.

  • pattern

    The parse tree pattern.

  • labels

    A mapping from label names to collections of

  • mismatchedNode

    The first node which failed to match the tree



18
19
20
# File 'lib/antlr4/tree/ParseTreeMatch.rb', line 18

def pattern
  @pattern
end

#treeObject

Constructs a new instance of ParseTreeMatch from the specified parse tree and pattern.

ParseTree objects located by the tree pattern matching process. pattern during the matching process.

Parameters:

  • tree

    The parse tree to match against the pattern.

  • pattern

    The parse tree pattern.

  • labels

    A mapping from label names to collections of

  • mismatchedNode

    The first node which failed to match the tree



18
19
20
# File 'lib/antlr4/tree/ParseTreeMatch.rb', line 18

def tree
  @tree
end

Instance Method Details

#get(label) ⇒ Object

Get the last node associated with a specific label.

<p>For example, for pattern <id:ID>, get(“id”) returns the node matched for that ID. If more than one node matched the specified label, only the last is returned. If there is no node associated with the label, this returns null.</p>

<p>Pattern tags like <ID> and <expr> without labels are considered to be labeled with ID and expr, respectively.</p>

label, or null if no parse tree matched a tag with the label.

Parameters:

  • label

    The label to check.

Returns:

  • The last ParseTree to match a tag with the specified



44
45
46
47
48
49
50
51
# File 'lib/antlr4/tree/ParseTreeMatch.rb', line 44

def get(label)
    parseTrees = self.labels.get(label, nil)
    if parseTrees.nil? or parseTrees.empty? then
        return nil
    else
        return parseTrees[-1]
    end
end

#getAll(label) ⇒ Object

Return all nodes matching a rule or token tag with the specified label.

<p>If the label is the name of a parser rule or token in the grammar, the resulting list will contain both the parse trees matching rule or tags explicitly labeled with the label and the complete set of parse trees matching the labeled and unlabeled tags in the pattern for the parser rule or token. For example, if label is “foo”, the result will contain all of the following.</p>

<ul> <li>Parse tree nodes matching tags of the form <foo:anyRuleName> and <foo:AnyTokenName>.</li> <li>Parse tree nodes matching tags of the form <anyLabel:foo>.</li> <li>Parse tree nodes matching tags of the form <foo>.</li> </ul>

the specified label. If no nodes matched the label, an empty list is returned.

Parameters:

  • label

    The label.

Returns:

  • A collection of all ParseTree nodes matching tags with



75
76
77
# File 'lib/antlr4/tree/ParseTreeMatch.rb', line 75

def getAll(label)
    self.labels.get(label, Array.new)
end

#succeeded@code true

Gets a value indicating whether the match operation succeeded.

false.

Returns:

  • (@code true)

    if the match operation succeeded; otherwise,



85
86
87
# File 'lib/antlr4/tree/ParseTreeMatch.rb', line 85

def succeeded
    return self.mismatchedNode.nil?
end

#to_sObject

@inheritDoc



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/antlr4/tree/ParseTreeMatch.rb', line 91

def to_s
    StringIO.open do |buf|
        buf.write("Match ")
        if self.succeeded() 
            buf.write("succeeded") 
        else 
            buf.write("failed")
        end
        buf.write("; found ")
        buf.write(self.labels.length.to_s)
        buf.write(" labels")
        return buf.string
    end
end