-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from junk0612/use-racc
Replace parser with one made by Racc
- Loading branch information
Showing
9 changed files
with
1,517 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
require "bundler/gem_tasks" | ||
|
||
namespace "build" do | ||
desc "build parser from parser.y by using Racc" | ||
task :racc_parser do | ||
`bundle exec racc parser.y -o lib/lrama/new_parser.rb` | ||
end | ||
|
||
desc "build parser for debugging" | ||
task :racc_verbose_parser do | ||
`bundle exec racc parser.y -o lib/lrama/new_parser.rb -t -v` | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
require "strscan" | ||
|
||
module Lrama | ||
class NewLexer | ||
attr_accessor :status | ||
attr_accessor :end_symbol | ||
|
||
def initialize(text) | ||
@scanner = StringScanner.new(text) | ||
@head = @scanner.pos | ||
@line = 1 | ||
@status = :initial | ||
@end_symbol = nil | ||
end | ||
|
||
def next_token | ||
case @status | ||
when :initial | ||
lex_token | ||
when :c_declaration | ||
lex_c_code | ||
end | ||
end | ||
|
||
def line | ||
@line | ||
end | ||
|
||
def col | ||
@scanner.pos - @head | ||
end | ||
|
||
def lex_token | ||
while !@scanner.eos? do | ||
case | ||
when @scanner.scan(/\n/) | ||
@line += 1 | ||
@head = @scanner.pos + 1 | ||
when @scanner.scan(/\s+/) | ||
# noop | ||
when @scanner.scan(/\/\*/) | ||
lex_comment | ||
when @scanner.scan(/%{/) | ||
return [@scanner.matched, @scanner.matched] | ||
when @scanner.scan(/%}/) | ||
return [@scanner.matched, @scanner.matched] | ||
when @scanner.scan(/%%/) | ||
return [@scanner.matched, @scanner.matched] | ||
when @scanner.scan(/{/) | ||
return [@scanner.matched, @scanner.matched] | ||
when @scanner.scan(/}/) | ||
return [@scanner.matched, @scanner.matched] | ||
when @scanner.scan(/\[/) | ||
return [@scanner.matched, @scanner.matched] | ||
when @scanner.scan(/\]/) | ||
return [@scanner.matched, @scanner.matched] | ||
when @scanner.scan(/:/) | ||
return [@scanner.matched, @scanner.matched] | ||
when @scanner.scan(/\|/) | ||
return [@scanner.matched, @scanner.matched] | ||
when @scanner.scan(/;/) | ||
return [@scanner.matched, @scanner.matched] | ||
when @scanner.scan(/%union|%token|%type|%left|%right|%nonassoc|%expect|%define|%require|%printer|%lex-param|%parse-param|%initial-action|%prec|%error-token/) | ||
return [@scanner.matched, @scanner.matched] | ||
when @scanner.scan(/<\w+>/) | ||
return [:TAG, @scanner.matched] | ||
when @scanner.scan(/'.'/) | ||
return [:CHARACTER, @scanner.matched] | ||
when @scanner.scan(/'\\\\'|'\\t'|'\\f'|'\\r'|'\\n'|'\\13'/) | ||
return [:CHARACTER, @scanner.matched] | ||
when @scanner.scan(/"/) | ||
return [:STRING, @scanner.scan_until(/"/)[0..-2]] | ||
when @scanner.scan(/\d+/) | ||
return [:INTEGER, @scanner.matched] | ||
when @scanner.scan(/([a-zA-Z_.][-a-zA-Z0-9_.]*)/) | ||
return [:IDENTIFIER, @scanner.matched] | ||
else | ||
raise | ||
end | ||
end | ||
end | ||
|
||
def lex_c_code | ||
nested = 0 | ||
code = '' | ||
while !@scanner.eos? do | ||
case | ||
when @scanner.scan(/{/) | ||
code += @scanner.matched | ||
nested += 1 | ||
when @scanner.scan(/}/) | ||
if nested == 0 && @end_symbol == '}' | ||
@scanner.unscan | ||
return [:C_DECLARATION, code] | ||
else | ||
code += @scanner.matched | ||
nested -= 1 | ||
end | ||
when @scanner.check(/#{@end_symbol}/) | ||
return [:C_DECLARATION, code] | ||
when @scanner.scan(/\n/) | ||
code += @scanner.matched | ||
@line += 1 | ||
@head = @scanner.pos + 1 | ||
when @scanner.scan(/"/) | ||
matched = @scanner.scan_until(/"/)[0..-2] | ||
code += %Q("#{matched}") | ||
@line += matched.count("\n") | ||
else | ||
code += @scanner.getch | ||
end | ||
end | ||
raise | ||
end | ||
|
||
def lex_comment | ||
while !@scanner.eos? do | ||
case | ||
when @scanner.scan(/\n/) | ||
@line += 1 | ||
@head = @scanner.pos + 1 | ||
when @scanner.scan(/\*\//) | ||
return | ||
else | ||
@scanner.getch | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.