Skip to content

Commit 6e108ec

Browse files
committed
Lexer now returns column info for every token
1 parent 7a36c3f commit 6e108ec

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/opal/fragment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def initialize(code, sexp = nil)
1616
# In debug mode we may wish to include the original line as a comment
1717
def to_code
1818
if @sexp
19-
"/*:#{@sexp.line}*/#{@code}"
19+
"/*:#{@sexp.line}:#{@sexp.column}*/#{@code}"
2020
else
2121
@code
2222
end

lib/opal/parser/lexer.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def initialize(source, file)
1616
@cmdarg = 0
1717
@line = 1
1818
@column = 0
19+
@tok_column = 0
1920
@file = file
2021

2122
@scanner = StringScanner.new(source)
@@ -84,14 +85,20 @@ def set_arg_state
8485

8586
def scan(regexp)
8687
if result = @scanner.scan(regexp)
88+
@column += result.length
8789
@yylval += @scanner.matched
8890
end
8991

9092
result
9193
end
9294

9395
def skip(regexp)
94-
@scanner.scan regexp
96+
if result = @scanner.scan(regexp)
97+
@column += result.length
98+
@tok_column = @column
99+
end
100+
101+
result
95102
end
96103

97104
def check(regexp)
@@ -106,10 +113,15 @@ def matched
106113
@scanner.matched
107114
end
108115

116+
def line=(line)
117+
@column = @tok_column = 0
118+
@line = line
119+
end
120+
109121
def next_token
110122
token = self.yylex
111123
value = self.yylval
112-
location = [@line, @column]
124+
location = [@line, @tok_column]
113125

114126
[token, [value, location]]
115127
end
@@ -547,10 +559,17 @@ def yylex
547559

548560
elsif skip(/(\n|#)/)
549561
c = scanner.matched
550-
if c == '#' then skip(/(.*)/) else @line += 1; end
562+
if c == '#'
563+
skip(/(.*)/)
564+
else
565+
self.line += 1
566+
end
551567

552568
skip(/(\n+)/)
553-
@line += scanner.matched.length if scanner.matched
569+
570+
if scanner.matched
571+
self.line += scanner.matched.length
572+
end
554573

555574
next if [:expr_beg, :expr_dot].include? @lex_state
556575

0 commit comments

Comments
 (0)