Skip to content

Commit

Permalink
Every token from lexer stores it original line number (and column)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Nov 22, 2013
1 parent fca7065 commit 19a08f4
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 39 deletions.
8 changes: 3 additions & 5 deletions lib/opal/compiler.rb
Expand Up @@ -107,9 +107,7 @@ def parser_indent
# returns an array, it must be used incase the internal structure
# of sexps does change.
def s(*parts)
sexp = Sexp.new(parts)
sexp.line = @line
sexp
Sexp.new(parts)
end

def fragment(str, sexp = nil)
Expand Down Expand Up @@ -181,7 +179,7 @@ def in_while?
# and compiling it to fragments.
def process(sexp, level = :expr)
if handler = handlers[sexp.type]
@line = sexp.line
#@line = sexp.line
return handler.new(sexp, level, self).compile_to_fragments
else
raise "Unsupported sexp: #{sexp.type}"
Expand Down Expand Up @@ -268,7 +266,7 @@ def returns(sexp)
sexp
else
s(:js_return, sexp).tap { |s|
s.line = sexp.line
#s.line = sexp.line
}
end
end
Expand Down
14 changes: 8 additions & 6 deletions lib/opal/parser.rb
Expand Up @@ -434,7 +434,6 @@ def new_op_asgn(op, lhs, rhs)

end

result.line = lhs.line
result
end

Expand Down Expand Up @@ -529,11 +528,14 @@ def new_var_ref(ref)
# returns for __FILE__ as it is converted into str
ref
when :identifier
if scope.has_local? ref[1]
s(:lvar, ref[1])
else
s(:call, nil, ref[1], s(:arglist))
end
result = if scope.has_local? ref[1]
s(:lvar, ref[1])
else
s(:call, nil, ref[1], s(:arglist))
end

result.loc = ref.loc
result
else
raise "Bad var_ref type: #{ref.type}"
end
Expand Down
12 changes: 0 additions & 12 deletions lib/opal/parser/grammar.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions lib/opal/parser/grammar.y
Expand Up @@ -175,12 +175,10 @@ rule
| expr kAND expr
{
result = s(:and, val[0], val[2])
result.line = val[0].line
}
| expr kOR expr
{
result = s(:or, val[0], val[2])
result.line = val[0].line
}
| kNOT expr
{
Expand Down Expand Up @@ -723,7 +721,6 @@ rule
bodystmt kEND
{
result = s(:begin, val[2])
result.line = val[1]
}
| tLPAREN_ARG expr opt_nl tRPAREN
{
Expand Down Expand Up @@ -816,7 +813,6 @@ rule
compstmt kEND
{
result = s(:while, val[2], val[5])
result.line = val[1]
}
| kUNTIL
{
Expand All @@ -830,22 +826,18 @@ rule
compstmt kEND
{
result = s(:until, val[2], val[5])
result.line = val[1]
}
| kCASE expr_value opt_terms case_body kEND
{
result = s(:case, val[1], *val[3])
result.line = val[1].line
}
| kCASE opt_terms case_body kEND
{
result = s(:case, nil, *val[2])
# result.line = val[2].line
}
| kCASE opt_terms kELSE compstmt kEND
{
result = s(:case, nil, val[3])
# result.line = val[3].line
}
| kFOR mlhs kIN
{
Expand Down Expand Up @@ -1065,7 +1057,6 @@ opt_block_args_tail: tCOMMA block_args_tail
opt_block_var compstmt kEND
{
result = new_iter val[2], val[3]
result.line = val[1]
pop_scope
}

Expand Down Expand Up @@ -1114,7 +1105,6 @@ opt_block_args_tail: tCOMMA block_args_tail
opt_block_var compstmt tRCURLY
{
result = new_iter val[2], val[3]
result.line = val[1]
pop_scope
}
| kDO
Expand All @@ -1125,7 +1115,6 @@ opt_block_args_tail: tCOMMA block_args_tail
opt_block_var compstmt kEND
{
result = new_iter val[2], val[3]
result.line = val[1]
pop_scope
}

Expand All @@ -1136,7 +1125,6 @@ opt_block_args_tail: tCOMMA block_args_tail
args then compstmt cases
{
part = s(:when, s(:array, *val[2]), val[4])
#part.line = val[2].line
result = [part]
result.push *val[5] if val[5]
}
Expand Down
3 changes: 2 additions & 1 deletion lib/opal/parser/lexer.rb
Expand Up @@ -13,6 +13,7 @@ def initialize(source, file)
@cond = 0
@cmdarg = 0
@line = 1
@column = 0
@file = file

@scanner = StringScanner.new(source)
Expand Down Expand Up @@ -97,7 +98,7 @@ def matched

def next_token
old = self.yylex
loc = []
loc = [@line, @column]
[old[0], [old[1], loc]]
end

Expand Down
16 changes: 13 additions & 3 deletions lib/opal/parser/sexp.rb
@@ -1,8 +1,6 @@
module Opal
class Sexp

attr_accessor :line
attr_accessor :end_line
attr_reader :array

attr_accessor :loc
Expand Down Expand Up @@ -55,8 +53,20 @@ def ==(other)

alias eql? ==

def line
@loc && @loc[0]
end

def column
@loc && @loc[1]
end

def inspect
"(#{@array.map { |e| e.inspect }.join ' '})"
"(#{@array.map { |e| e.inspect }.join ', '})"
end

def pretty_inspect
"(#{line ? "#{line} " : ''}#{@array.map { |e| e.inspect }.join ', '})"
end

alias to_s inspect
Expand Down

0 comments on commit 19a08f4

Please sign in to comment.