Add helper function on library module

This commit is contained in:
Glenn Y. Rolland 2019-12-21 20:34:47 +01:00
parent 43989e6e48
commit a10d8db8a9
7 changed files with 33 additions and 6 deletions

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'namarara/version'
module Namarara; end
@ -9,3 +11,19 @@ require 'namarara/parser'
require 'namarara/eval_error'
require 'namarara/errors/var_not_defined'
require 'namarara/errors/invalid_grammar'
module Namarara
def self.parse_string(line, vars, debug = false)
parser = Parser.new(Lexer.new)
parser.names = vars
parser_bet = parser.parse(line.chomp, debug)
parser.check_grammar line, parser_bet
{
expr: line,
tree: parser_bet.to_s,
errors: parser_bet&.errors&.map { |e| e.message },
result: parser_bet&.compute
}
end
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
module Namarara
module Errors
class InvalidGrammar < EvalError

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
module Namarara
module Errors
class VarNotDefined < EvalError

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
module Namarara
class EvalError
attr_accessor :var

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
module Namarara
class Lexer < Rly::Lex
attr_reader :logger
@ -27,11 +29,9 @@ module Namarara
end
on_error do |t|
unless t.lexer.logger.nil?
t.lexer.logger.error "Illegal character #{t.value}"
end
t.lexer.logger&.error "Illegal character #{t.value}"
t.lexer.pos += 1
nil
end
end # class
end # module
end
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
module Namarara
VERSION = '0.9.4'.freeze
VERSION = '0.9.4'
end

View file

@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'thor'
spec.add_development_dependency 'rly'
spec.add_development_dependency 'sinatra'
spec.add_development_dependency "bundler", "~> 1.15"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest", "~> 5.0"