Module: Xel::Parser

Includes:
Raabro
Defined in:
lib/xel/parser.rb

Instance Method Summary collapse

Instance Method Details

#aa(i) ⇒ Object

parse



8
# File 'lib/xel/parser.rb', line 8

def aa(i); rex(nil, i, /\{\s*/); end

#add(i) ⇒ Object



38
# File 'lib/xel/parser.rb', line 38

def add(i); jseq(:add, i, :mul, :adder); end

#adder(i) ⇒ Object



32
# File 'lib/xel/parser.rb', line 32

def adder(i); rex(:adder, i, /[+\-&]\s*/); end

#arr(i) ⇒ Object



20
# File 'lib/xel/parser.rb', line 20

def arr(i); eseq(:arr, i, :aa, :cmp, :com, :az); end

#az(i) ⇒ Object



9
# File 'lib/xel/parser.rb', line 9

def az(i); rex(nil, i, /\}\s*/); end

#cmp(i) ⇒ Object



41
# File 'lib/xel/parser.rb', line 41

def cmp(i); seq(:cmp, i, :add, :rcmp, '?'); end

#com(i) ⇒ Object



12
# File 'lib/xel/parser.rb', line 12

def com(i); rex(nil, i, /,\s*/); end

#comparator(i) ⇒ Object



30
# File 'lib/xel/parser.rb', line 30

def comparator(i); rex(:comparator, i, /([\<\>]=?|=~|!?=|IN)\s*/); end

#dqstring(i) ⇒ Object



23
# File 'lib/xel/parser.rb', line 23

def dqstring(i); rex(:dqstring, i, /"(\\"|[^"])*"\s*/); end

#exp(i) ⇒ Object



35
# File 'lib/xel/parser.rb', line 35

def exp(i); alt(:exp, i, :par, :fun, :number, :string, :arr, :var); end

#fun(i) ⇒ Object



28
# File 'lib/xel/parser.rb', line 28

def fun(i); seq(:fun, i, :funname, :funargs); end

#funargs(i) ⇒ Object



26
# File 'lib/xel/parser.rb', line 26

def funargs(i); eseq(:funargs, i, :pa, :cmp, :com, :pz); end

#funname(i) ⇒ Object



27
# File 'lib/xel/parser.rb', line 27

def funname(i); rex(:funname, i, /[_a-zA-Z][_a-zA-Z0-9]*/); end

#mul(i) ⇒ Object



37
# File 'lib/xel/parser.rb', line 37

def mul(i); jseq(:mul, i, :exp, :multiplier); end

#multiplier(i) ⇒ Object



31
# File 'lib/xel/parser.rb', line 31

def multiplier(i); rex(:multiplier, i, /[*\/]\s*/); end

#number(i) ⇒ Object



14
15
16
# File 'lib/xel/parser.rb', line 14

def number(i)
  rex(:number, i, /-?(\.[0-9]+|([0-9][,0-9]*[0-9]|[0-9]+)(\.[0-9]+)?)\s*/)
end

#pa(i) ⇒ Object



10
# File 'lib/xel/parser.rb', line 10

def pa(i); rex(nil, i, /\(\s*/); end

#par(i) ⇒ Object



34
# File 'lib/xel/parser.rb', line 34

def par(i); seq(:par, i, :pa, :cmp, :pz); end

#prequal(i) ⇒ Object



43
# File 'lib/xel/parser.rb', line 43

def prequal(i); rex(nil, i, /\s*=?\s*/); end

#pz(i) ⇒ Object



11
# File 'lib/xel/parser.rb', line 11

def pz(i); rex(nil, i, /\)\s*/); end

#qstring(i) ⇒ Object



22
# File 'lib/xel/parser.rb', line 22

def qstring(i); rex(:qstring, i, /'(\\'|[^'])*'\s*/); end

#rcmp(i) ⇒ Object



40
# File 'lib/xel/parser.rb', line 40

def rcmp(i); seq(:rcmp, i, :comparator, :add); end

#rewrite_add(tree) ⇒ Object Also known as: rewrite_mul



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/xel/parser.rb', line 58

def rewrite_add(tree)

  return rewrite(tree.children.first) if tree.children.size == 1

  cn = tree.children.dup
  a = [ tree.name == :add ? 'plus' : 'MUL' ]
  a = [ 'amp' ] if cn[1] && cn[1].strinp == '&'
  mod = nil

  while c = cn.shift
    v = rewrite(c)
    v = [ mod, v ] if mod
    a << v
    c = cn.shift
    break unless c
    mod = { '-' => 'opp', '/' => 'inv' }[c.string.strip]
  end

  a
end

#rewrite_arr(tree) ⇒ Object



94
95
96
97
98
# File 'lib/xel/parser.rb', line 94

def rewrite_arr(tree)

  [ 'arr',
    *tree.children.inject([]) { |a, c| a << rewrite(c) if c.name; a } ]
end

#rewrite_cmp(tree) ⇒ Object

rewrite



48
49
50
51
52
53
54
55
56
# File 'lib/xel/parser.rb', line 48

def rewrite_cmp(tree)

  return rewrite(tree.children.first) if tree.children.size == 1

  [ 'cmp',
    tree.children[1].children.first.string.strip,
    rewrite(tree.children[0]),
    rewrite(tree.children[1].children[1]) ]
end

#rewrite_exp(tree) ⇒ Object



91
# File 'lib/xel/parser.rb', line 91

def rewrite_exp(tree); rewrite(tree.children[0]); end

#rewrite_fun(tree) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/xel/parser.rb', line 80

def rewrite_fun(tree)

  t =
    [ tree.children[0].string ] +
    tree.children[1].children.select(&:name).collect { |c| rewrite(c) }
  class << t; attr_accessor :_source; end
  t._source = tree.strinp

  t
end

#rewrite_number(tree) ⇒ Object



101
# File 'lib/xel/parser.rb', line 101

def rewrite_number(tree); [ 'num', tree.string.strip ]; end

#rewrite_par(tree) ⇒ Object



92
# File 'lib/xel/parser.rb', line 92

def rewrite_par(tree); rewrite(tree.children[1]); end

#rewrite_string(tree) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/xel/parser.rb', line 103

def rewrite_string(tree)

  s = tree.children[0].string.strip
  q = s[0]
  s = s[1..-2]

  [ 'str', q == '"' ? s.gsub("\\\"", '"') : s.gsub("\\'", "'") ]
end

#rewrite_var(tree) ⇒ Object



100
# File 'lib/xel/parser.rb', line 100

def rewrite_var(tree); [ 'var', tree.string.strip ]; end

#root(i) ⇒ Object



44
# File 'lib/xel/parser.rb', line 44

def root(i); seq(nil, i, :prequal, :cmp); end

#string(i) ⇒ Object



24
# File 'lib/xel/parser.rb', line 24

def string(i); alt(:string, i, :dqstring, :qstring); end

#var(i) ⇒ Object



18
# File 'lib/xel/parser.rb', line 18

def var(i); rex(:var, i, /[a-z_][A-Za-z0-9_.]*\s*/); end