Method: Lrama::Grammar#add_term

Defined in:
lib/lrama/grammar.rb

#add_term(id:, alias_name: nil, tag: nil, token_id: nil, replace: false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lrama/grammar.rb', line 52

def add_term(id:, alias_name: nil, tag: nil, token_id: nil, replace: false)
  if token_id && (sym = @symbols.find {|s| s.token_id == token_id })
    if replace
      sym.id = id
      sym.alias_name = alias_name
      sym.tag = tag
    end

    return sym
  end

  if sym = @symbols.find {|s| s.id == id }
    return sym
  end

  sym = Symbol.new(
    id: id, alias_name: alias_name, number: nil, tag: tag,
    term: true, token_id: token_id, nullable: false
  )
  @symbols << sym
  @terms = nil

  return sym
end