Class: Rubyang::Xpath::Parser

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/rubyang/xpath/parser.rb,
lib/rubyang/xpath/parser/parser.tab.rb

Constant Summary collapse

Racc_arg =
[
racc_action_table,
racc_action_check,
racc_action_default,
racc_action_pointer,
racc_goto_table,
racc_goto_check,
racc_goto_default,
racc_goto_pointer,
racc_nt_base,
racc_reduce_table,
racc_token_table,
racc_shift_n,
racc_reduce_n,
racc_use_result_var ]
Racc_token_to_s_table =
[
"$end",
"error",
"\"/\"",
"\"::\"",
"\"ancestor\"",
"\"ancestor-or-self\"",
"\"attribute\"",
"\"child\"",
"\"descendant\"",
"\"descendant-or-self\"",
"\"following\"",
"\"following-sibling\"",
"\"namespace\"",
"\"parent\"",
"\"preceding\"",
"\"preceding-sibling\"",
"\"self\"",
"\"(\"",
"\")\"",
"\"processing-instruction\"",
"\"Literal\"",
"\"[\"",
"\"]\"",
"\"//\"",
"\".\"",
"\"..\"",
"\"@\"",
"\",\"",
"\"Exp\"",
"\"|\"",
"\"FilterExpr Predicat\"",
"\"or\"",
"\"and\"",
"\"=\"",
"\"!=\"",
"\"<\"",
"\">\"",
"\"<=\"",
"\">=\"",
"\"+\"",
"\"-\"",
"\"*\"",
"\"div\"",
"\"mod\"",
"\"UnaryExp\"",
"\"Digits\"",
"\"last(\"",
"\"position(\"",
"\"count(\"",
"\"id(\"",
"\"local-name(\"",
"\"namespace-uri(\"",
"\"name(\"",
"\"string(\"",
"\"concat(\"",
"\"starts-with(\"",
"\"contains(\"",
"\"substring-before(\"",
"\"substring-after(\"",
"\"substring(\"",
"\"string-length(\"",
"\"normalize-space(\"",
"\"translate(\"",
"\"boolean(\"",
"\"not(\"",
"\"true(\"",
"\"false(\"",
"\"lang(\"",
"\"number(\"",
"\"sum(\"",
"\"floor(\"",
"\"ceiling(\"",
"\"round(\"",
"\"current(\"",
"\"$\"",
"\"NCName\"",
"\":\"",
"\"comment\"",
"\"text\"",
"\"node\"",
"$start",
"statement",
"\"Expr\"",
"\"LocationPath\"",
"\"RelativeLocationPath\"",
"\"AbsoluteLocationPath\"",
"\"AbbreviatedAbsoluteLocationPath\"",
"\"Step\"",
"\"AbbreviatedRelativeLocationPath\"",
"\"AxisSpecifier\"",
"\"NodeTest\"",
"\"Predicates\"",
"\"AbbreviatedStep\"",
"\"AxisName\"",
"\"AbbreviatedAxisSpecifier\"",
"\"Predicate\"",
"\"NameTest\"",
"\"NodeType\"",
"\"PredicateExpr\"",
"\"OrExpr\"",
"\"PrimaryExpr\"",
"\"VariableReference\"",
"\"Number\"",
"\"FunctionCall\"",
"\"FunctionName(\"",
"\"Arguments\"",
"\"Argument\"",
"\"UnionExpr\"",
"\"PathExpr\"",
"\"FilterExpr\"",
"\"AndExpr\"",
"\"EqualityExpr\"",
"\"RelationalExpr\"",
"\"AdditiveExpr\"",
"\"MultiplicativeExpr\"",
"\"UnaryExpr\"",
"\"QName\"",
"\"PrefixedName\"",
"\"UnprefixedName\"",
"\"Prefix\"",
"\"LocalPart\"" ]
Racc_debug_parser =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



16
17
18
# File 'lib/rubyang/xpath/parser.rb', line 16

def initialize
  @logger = Logger.new(self.class.name)
end

Class Method Details

.parse(xpath_str) ⇒ Object



11
12
13
14
# File 'lib/rubyang/xpath/parser.rb', line 11

def self.parse( xpath_str )
  parser = self.new
  parser.parse( xpath_str )
end

Instance Method Details

#_reduce_none(val, _values, result) ⇒ Object



2037
2038
2039
# File 'lib/rubyang/xpath/parser/parser.tab.rb', line 2037

def _reduce_none(val, _values, result)
  val[0]
end

#next_tokenObject



124
125
126
127
# File 'lib/rubyang/xpath/parser.rb', line 124

def next_token
  @logger.debug { @tokens.first }
  @tokens.shift
end

#parse(xpath_str) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rubyang/xpath/parser.rb', line 20

def parse( xpath_str )
  @tokens = Array.new

  s = StringScanner.new( xpath_str )

  scanre_list = [
    ["(",                      /^\(/],
    [")",                      /^\)/],
    ["[",                      /^\[/],
    ["]",                      /^\]/],
    ["..",                     /^\.\./],
    [".",                      /^\./],
    ["@",                      /^\@/],
    [",",                      /^\,/],
    ["::",                     /^\:\:/],
    ["Literal",                /^(?:"[^"]*"|'[^']*')/],
    ["Digits",                 /^[0-9]+/],
    ["//",                     /^\/\//],
    ["/",                      /^\//],
    ["|",                      /^\|/],
    ["+",                      /^\+/],
    ["-",                      /^\-/],
    ["!=",                     /^\!\=/],
    ["<=",                     /^\<\=/],
    [">=",                     /^\>\=/],
    ["=",                      /^\=/],
    ["<",                      /^\</],
    [">",                      /^\>/],
    ["and",                    /^and/],
    ["or",                     /^or/],
    ["mod",                    /^mod/],
    ["div",                    /^div/],
    ["$",                      /^\$/],
    ["*",                      /^\*/],
    [":",                      /^\:/],
    ["S",                      /^[ \t]+/],
    # NodeType
    ["comment",                /^comment/],
    ["text",                   /^text/],
    ["node",                   /^node/],
    # ProcessingInstruction
    ["processing-instruction", /^processing-instruction/],
    # Axis
    ["ancestor",               /^ancestor/],
    ["ancestor-or-self",       /^ancestor-or-self/],
    ["attribute",              /^attribute/],
    ["child",                  /^child/],
    ["descendant",             /^descendant/],
    ["descendant-or-self",     /^descendant-or-self/],
    ["following",              /^following/],
    ["following-sibling",      /^following-sibling/],
    ["namespace",              /^namespace/],
    ["parent",                 /^parent/],
    ["preceding",              /^preceding/],
    ["preceding-sibling",      /^preceding-sibling/],
    ["self",                   /^self/],
    # FunctionName
    ["last(",                  /^last[ \t]*\(/],
    ["position(",              /^position[ \t]*\(/],
    ["count(",                 /^count[ \t]*\(/],
    ["id(",                    /^id[ \t]*\(/],
    ["local-name(",            /^local-name[ \t]*\(/],
    ["namespace-uri(",         /^namespace-uri[ \t]*\(/],
    ["name(",                  /^name[ \t]*\(/],
    ["string(",                /^string[ \t]*\(/],
    ["concat(",                /^concat[ \t]*\(/],
    ["starts-with(",           /^starts-with[ \t]*\(/],
    ["contains(",              /^contains[ \t]*\(/],
    ["substring-before(",      /^substring-before[ \t]*\(/],
    ["substring-after(",       /^substring-after[ \t]*\(/],
    ["substring(",             /^substring[ \t]*\(/],
    ["string-length(",         /^string-length[ \t]*\(/],
    ["normalize-space(",       /^normalize-space[ \t]*\(/],
    ["translate(",             /^translate[ \t]*\(/],
    ["boolean(",               /^boolean[ \t]*\(/],
    ["not(",                   /^not[ \t]*\(/],
    ["true(",                  /^true[ \t]*\(/],
    ["false(",                 /^false[ \t]*\(/],
    ["lang(",                  /^lang[ \t]*\(/],
    ["number(",                /^number[ \t]*\(/],
    ["sum(",                   /^sum[ \t]*\(/],
    ["floor(",                 /^floor[ \t]*\(/],
    ["ceiling(",               /^ceiling[ \t]*\(/],
    ["round(",                 /^round[ \t]*\(/],
    ["current(",               /^current[ \t]*\(/],
    # NCName
    ["NCName",                 /^(?:[A-Z]|\_|[a-z])(?:[A-Z]|\_|[a-z]|\-|\.|[0-9])*/],
  ]

  scanre = Regexp.union( scanre_list.map{ |scanre| scanre[1] } )

  until s.eos?
    token = s.scan( scanre )
    @logger.debug { token }
    next if "S" == scanre_list.find{ |s| s[1] =~ token }[0]
    @tokens.push [scanre_list.find{ |s| s[1] =~ token }[0], token]
    @logger.debug { @tokens.last }
  end

  @logger.debug { 'run do_parse' }
  result = self.do_parse
  result
end