Module: JPTType

Included in:
JPT, Treetop::Runtime::SyntaxNode
Defined in:
lib/jpt.rb

Constant Summary collapse

FUNCSIG_CHARS =
{"l" => :logical, "n" => :nodes, "v" => :value}
FUNCTABLE =
{
  "length" => [:value, :value],
  "count" => [:value, :nodes],
  "match" => [:logical, :value, :value],
  "search" => [:logical, :value, :value],
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_funcsig(name, sig) ⇒ Object



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

def self.add_funcsig(name, sig)
  FUNCTABLE[name] = sig.chars.map {FUNCSIG_CHARS[_1]}
end

Instance Method Details

#declared_as(ast, rt, s = "") ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jpt.rb', line 52

def declared_as(ast, rt, s = "")
  dt = declared_type(ast)
  case [dt, rt]
  in a, b if a == b
    true
  in [:nodes, :value]
    query_singular?(ast)
  in [:nodes, :logical]
    true
  else
    warn "*** Cannot use #{ast} with declared_type #{dt||:undefined} for required type #{rt}#{s}"
    false
  end
end

#declared_type(ast) ⇒ Object



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
# File 'lib/jpt.rb', line 25

def declared_type(ast)
  case ast
  in Numeric | String | false | true | nil
    :value
  in ["@", *] | ["$", *]
    :nodes
  in ["not", ast1]
    declared_as(ast1, :logical, " in #{ast}")
    :logical
  in ["and" | "or", *ast1]
    ast1.each do |astx|
      declared_as(astx, :logical, " in #{ast}")
    end
    :logical
  in ["func", funcname, *funcargs]
    ret, *parms = FUNCTABLE[funcname]
    if parms.length != funcargs.length
      warn "*** Incorrect number of arguments #{ast} #{parms.inspect} #{funcargs.inspect}"
    else
      parms.zip(funcargs).each do |pm, ar|
        declared_as(ar, pm, " in #{ast}") # XXX overhead
      end
    end
    ret
  end
end

#query_singular?(ast) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# File 'lib/jpt.rb', line 18

def query_singular?(ast)
  case ast
  in [("@" | "$"), *segments]
    ! segments.any? {|segment| Array === segment}
  end
end