Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Very basic syntax errors.

  • Loading branch information...
commit a95c699f390bf60e64a1107c5065c900af989b0d 1 parent 61df7d7
@brixen authored
View
39 lib/poison/bootstrap/parser.rb
@@ -1,13 +1,22 @@
module Poison
class Parser
def initialize
- @statements = []
- @call_list = []
- @str1 = ""
+ @error_position = 0
+ @statements = []
+ @call_list = []
+ @str1 = ""
+ @str2 = ""
end
# The #parse method is defined in the parser C extension.
+ def parse(string)
+ @string = string
+ ast = parse_string string
+ show_syntax_error unless ast
+ ast
+ end
+
def parse_file(name)
string = IO.read name
parse string
@@ -15,8 +24,28 @@ def parse_file(name)
# Parsing callbacks
- def syntax_error
- raise Syntax::SyntaxError
+ def show_syntax_error
+ error_line = nil
+ count = 0
+
+ @string.each_line do |line|
+ count += line.size
+ if count > @error_position
+ error_line = line
+ break
+ end
+ end
+
+ message = <<-EOM
+
+#{error_line.chomp}
+#{" " *(error_line.size - (count - @error_position))}^
+EOM
+ raise Syntax::SyntaxError, message
+ end
+
+ def syntax_error(pos)
+ @error_position = pos
end
def statement_start(statement)
View
8 lib/poison/bootstrap/parser/ext/parser.g
@@ -265,7 +265,7 @@ arg-sep = '.' - { }
%%
-VALUE poison_parse(VALUE self, VALUE string) {
+VALUE poison_parse_string(VALUE self, VALUE string) {
Poison P;
P.parser = self;
@@ -277,8 +277,8 @@ VALUE poison_parse(VALUE self, VALUE string) {
G->pos = G->limit = 0;
if (!poison_code_parse(G)) {
- rb_funcall(P.parser, rb_intern("syntax_error"), 0);
- return Qnil;
+ rb_funcall(P.parser, rb_intern("syntax_error"), 1, INT2FIX(G->end));
+ return P.ast = Qfalse;
}
poison_code_parse_free(G);
@@ -289,5 +289,5 @@ void Init_parser(void) {
VALUE rb_mPoison = rb_const_get(rb_cObject, rb_intern("Poison"));
VALUE rb_cParser = rb_const_get(rb_mPoison, rb_intern("Parser"));
- rb_define_method(rb_cParser, "parse", RUBY_METHOD_FUNC(poison_parse), 1);
+ rb_define_method(rb_cParser, "parse_string", RUBY_METHOD_FUNC(poison_parse_string), 1);
}
Please sign in to comment.
Something went wrong with that request. Please try again.