lib/ddbcli/ddb-parser.tab.rb in ddbcli-0.2.1 vs lib/ddbcli/ddb-parser.tab.rb in ddbcli-0.2.2

- old
+ new

@@ -90,10 +90,11 @@ end private :struct def scan tok = nil + @prev_tokens = [] until @ss.eos? if (tok = @ss.scan /\s+/) # nothing to do elsif (tok = @ss.scan /(?:<>|!=|>=|<=|>|<|=)/) @@ -131,22 +132,47 @@ yield [tok, tok] elsif (tok = @ss.scan /\|(?:.*)/) yield [:RUBY_SCRIPT, tok.slice(1..-1)] elsif (tok = @ss.scan /\!(?:.*)/) yield [:SHELL_SCRIPT, tok.slice(1..-1)] - elsif (tok = @ss.scan /[-.0-9a-z_]*/i) + elsif (tok = @ss.scan %r|[-.0-9a-z_:/]*|i) yield [:IDENTIFIER, tok] else - raise Racc::ParseError, ('parse error on value "%s"' % @ss.rest.inspect) + raise_error(tok, @prev_tokens, @ss) end + + @prev_tokens << tok end - yield [false, 'EOF'] + yield [false, ''] end private :scan +def raise_error(error_value, prev_tokens, scanner) + errmsg = ["__#{error_value}__"] + + if prev_tokens and not prev_tokens.empty? + toks = prev_tokens.reverse[0, 5].reverse + toks.unshift('...') if prev_tokens.length > toks.length + errmsg.unshift(toks.join.strip) unless toks.empty? + end + + if scanner and not (rest = (scanner.rest || '').strip).empty? + str = rest[0, 16] + str += '...' if rest.length > str.length + errmsg << str + end + + raise Racc::ParseError, ('parse error on value: %s' % errmsg.join(' ')) +end +private :raise_error + def parse yyparse self, :scan +end + +def on_error(error_token_id, error_value, value_stack) + raise_error(error_value, @prev_tokens, @ss) end def self.parse(obj) self.new(obj).parse end