namarara/lib/mm2ep_depend/parser.rb

256 lines
5 KiB
Ruby
Raw Normal View History

2017-09-04 09:19:29 +00:00
module Mm2ep
module Depend
2017-09-12 15:25:16 +00:00
class TreeExpr
def compute
raise NotImplementedError
end
end
2017-09-12 13:58:15 +00:00
2017-09-12 14:05:50 +00:00
class TreeValue
2017-09-15 10:52:29 +00:00
attr_reader :value
2017-09-12 14:32:03 +00:00
2017-09-12 15:25:16 +00:00
def to_s
2017-09-12 14:32:03 +00:00
raise NotImplementedError
2017-09-12 15:25:16 +00:00
end
end
2017-09-12 14:05:50 +00:00
class VarValue < TreeValue
attr_reader :errors
def initialize(str, value)
@errors = []
2017-09-12 13:58:15 +00:00
@name = str
2017-09-20 08:46:17 +00:00
@value = value
@value = true if value =~ /^true$/i
@value = false if value =~ /^false$/i
2017-09-15 10:52:29 +00:00
return unless @value.nil?
@errors << VarNotDefined.new(
message: "No value for #{@name}",
var: @name
)
2017-09-13 12:55:56 +00:00
end
def compute
@value
2017-09-12 15:25:16 +00:00
end
2017-09-12 14:32:03 +00:00
2017-09-12 15:25:16 +00:00
def to_s
2017-09-12 14:32:03 +00:00
"var:#{@name}<-(#{@value})"
end
2017-09-12 15:25:16 +00:00
end
2017-09-12 13:58:15 +00:00
2017-09-12 14:05:50 +00:00
class NumberValue < TreeValue
attr_reader :errors
def initialize(str)
@errors = []
@value = str
2017-09-12 15:25:16 +00:00
end
2017-09-12 14:32:03 +00:00
2017-09-13 12:55:56 +00:00
def compute
@value
end
2017-09-12 15:25:16 +00:00
def to_s
2017-09-12 14:32:03 +00:00
"number:#{@value}"
end
2017-09-12 15:25:16 +00:00
end
2017-09-12 14:05:50 +00:00
class StringValue < TreeValue
attr_reader :errors
def initialize(str)
@errors = []
2017-09-12 14:32:03 +00:00
@value = str
2017-09-12 15:25:16 +00:00
end
2017-09-12 14:32:03 +00:00
2017-09-13 12:55:56 +00:00
def compute
@value
end
2017-09-12 15:25:16 +00:00
def to_s
2017-09-12 14:32:03 +00:00
"string:\"#{@value}\""
end
2017-09-12 15:25:16 +00:00
end
2017-09-12 14:05:50 +00:00
class BoolValue < TreeValue
attr_reader :errors
def initialize(str)
@errors = []
2017-09-14 10:08:33 +00:00
@value = case str
when /true/i then true
when /false/i then false
end
2017-09-12 15:25:16 +00:00
end
2017-09-12 13:58:15 +00:00
2017-09-13 12:55:56 +00:00
def compute
@value
end
2017-09-12 15:25:16 +00:00
def to_s
2017-09-12 14:32:03 +00:00
"bool:#{@value}"
end
2017-09-12 15:25:16 +00:00
end
2017-09-12 13:58:15 +00:00
2017-09-12 14:05:50 +00:00
class AndOp < TreeExpr
attr_reader :errors
def initialize(lval, rval)
@errors = []
@errors.concat lval.errors
@errors.concat rval.errors
@lval = lval
@rval = rval
2017-09-11 12:33:29 +00:00
end
2017-09-12 13:58:15 +00:00
def compute
@lval.compute && @rval.compute
2017-09-11 12:33:29 +00:00
end
2017-09-12 14:32:03 +00:00
2017-09-12 15:25:16 +00:00
def to_s
"( #{@lval} ) AND ( #{@rval} )"
2017-09-12 15:25:16 +00:00
end
2017-09-11 12:33:29 +00:00
end
2017-09-12 13:58:15 +00:00
class OrOp
attr_reader :errors
def initialize(lval, rval)
@errors = []
@errors.concat lval.errors
@errors.concat rval.errors
@lval = lval
@rval = rval
2017-09-11 12:33:29 +00:00
end
2017-09-12 13:58:15 +00:00
def compute
@lval.compute || @rval.compute
2017-09-11 12:33:29 +00:00
end
2017-09-12 14:32:03 +00:00
2017-09-12 15:25:16 +00:00
def to_s
"( #{@lval} ) OR ( #{@rval} )"
2017-09-12 15:25:16 +00:00
end
2017-09-11 12:33:29 +00:00
end
2017-09-12 14:05:50 +00:00
class NotOp
attr_reader :errors
def initialize(expr)
@errors = []
@errors.concat expr.errors
2017-09-11 12:33:29 +00:00
@expr = expr
end
2017-09-12 13:58:15 +00:00
def compute
!@expr.compute
2017-09-11 12:33:29 +00:00
end
2017-09-12 14:32:03 +00:00
2017-09-12 15:25:16 +00:00
def to_s
"NOT ( #{@expr} )"
2017-09-12 15:25:16 +00:00
end
2017-09-11 12:33:29 +00:00
end
2017-09-12 13:58:15 +00:00
class EqOp
attr_reader :errors
def initialize(lval, rval)
@errors = []
@errors.concat lval.errors
@errors.concat rval.errors
2017-09-12 15:25:16 +00:00
@lval = lval
2017-09-12 14:23:44 +00:00
@rval = rval
2017-09-11 12:33:29 +00:00
end
2017-09-12 13:58:15 +00:00
def compute
2017-09-14 10:08:33 +00:00
@lval.value == @rval.value
2017-09-11 12:33:29 +00:00
end
2017-09-12 14:32:03 +00:00
2017-09-12 15:25:16 +00:00
def to_s
"#{@lval} = #{@rval}"
2017-09-12 15:25:16 +00:00
end
2017-09-11 12:33:29 +00:00
end
2017-09-07 15:05:06 +00:00
class Parser < Rly::Yacc
attr_writer :names
2017-09-15 10:52:29 +00:00
# Check if grammar is valid
2017-09-15 10:52:29 +00:00
def check_grammar(line, tokens)
grammar = tokens.to_s.split(/=|AND|OR/)
expr = line.split(/=|AND|OR/)
return if grammar.size == expr.size
return if grammar.empty?
tokens.errors << InvalidGrammar.new(
message: 'Invalid Grammar'
)
end
2017-09-13 12:55:56 +00:00
2017-09-12 15:52:38 +00:00
precedence :left, :OR_OP
precedence :left, :AND_OP
precedence :left, :EQ_OP
precedence :right, :L_PAR, :R_PAR
2017-09-12 15:09:53 +00:00
precedence :right, :UMINUS
2017-09-11 08:13:30 +00:00
2017-09-07 15:05:06 +00:00
rule 'statement : expr' do |st, e|
st.value = e.value
end
2017-09-11 08:13:30 +00:00
rule 'expr : VAR' do |ex, l|
2017-09-12 14:05:50 +00:00
ex.value = EqOp.new(
2017-09-13 12:55:56 +00:00
VarValue.new(l.value, @names[l.value]),
2017-09-12 14:05:50 +00:00
BoolValue.new('true')
)
2017-09-11 08:13:30 +00:00
end
rule 'bool_expr : F_BOOL' do |ex, l|
2017-09-12 15:46:04 +00:00
ex.value = l.value
end
rule 'bool_expr : T_BOOL' do |ex, l|
2017-09-12 15:46:04 +00:00
ex.value = l.value
2017-09-11 08:13:30 +00:00
end
2017-09-12 15:46:04 +00:00
rule 'expr : bool_expr' do |ex, l|
2017-09-12 14:16:57 +00:00
ex.value = BoolValue.new(l.value.to_s)
2017-09-11 08:13:30 +00:00
end
rule 'expr : expr OR_OP expr' do |ex, l, _e, r|
2017-09-12 15:52:38 +00:00
ex.value = OrOp.new(l.value, r.value)
2017-09-07 15:05:06 +00:00
end
rule 'expr : expr AND_OP expr' do |ex, l, _e, r|
2017-09-12 13:58:15 +00:00
ex.value = AndOp.new(l.value, r.value)
2017-09-07 15:05:06 +00:00
end
rule 'expr : L_PAR expr R_PAR' do |ex, _l, e, _r|
2017-09-11 12:33:29 +00:00
ex.value = e.value
2017-09-07 15:05:06 +00:00
end
rule 'expr : NOT_OP expr %prec UMINUS' do |ex, _l, e|
2017-09-12 15:53:24 +00:00
ex.value = NotOp.new(e.value)
end
rule 'expr : VAR EQ_OP bool_expr' do |ex, v, _eq, n|
2017-09-12 14:16:57 +00:00
ex.value = EqOp.new(
2017-09-13 12:55:56 +00:00
VarValue.new(v.value.to_s, @names[v.value]),
2017-09-12 14:16:57 +00:00
BoolValue.new(n.value)
)
2017-09-07 15:05:06 +00:00
end
rule 'expr : VAR EQ_OP STRING' do |ex, v, _eq, n|
2017-09-12 14:16:57 +00:00
ex.value = EqOp.new(
2017-09-13 12:55:56 +00:00
VarValue.new(v.value.to_s, @names[v.value]),
2017-09-12 14:16:57 +00:00
StringValue.new(n.value)
)
2017-09-07 15:05:06 +00:00
end
rule 'expr : VAR EQ_OP NUMBER' do |ex, v, _eq, n|
2017-09-12 14:16:57 +00:00
ex.value = EqOp.new(
2017-09-13 12:55:56 +00:00
VarValue.new(v.value.to_s, @names[v.value]),
2017-09-12 14:16:57 +00:00
NumberValue.new(n.value)
)
2017-09-07 15:05:06 +00:00
end
2017-09-04 09:19:29 +00:00
end # class
end # module
end # module