8000 Lua scanner, tweaked (finally!) by korny · Pull Request #141 · rubychan/coderay · GitHub
[go: up one dir, main page]

Skip to content

Lua scanner, tweaked (finally!) #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jun 22, 2013
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions lib/coderay/scanners/lua.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ def setup
def scan_tokens(encoder, options)
@encoder = encoder
@options = options

send(:"handle_state_#@state") until eos?

@encoder
end

def handle_state_initial

until eos?
case state

when :initial
if match = scan(/\-\-\[\=*\[/) #--[[ long (possibly multiline) comment ]]
@num_equals = match.count("=") # Number must match for comment end
@encoder.begin_group(:comment)
Expand Down Expand Up @@ -146,9 +144,8 @@ def handle_state_initial
# (tables can contain full expressions in parts).
# If this is the case, return to :table scanning state.
@state = :table if @state == :initial && @brace_depth >= 1
end

def handle_state_function_expected

when :function_expected
if match = scan(/\(.*?\)/m) # x = function() # "Anonymous" function without explicit name
@encoder.text_token(match, :operator)
@state = :initial
Expand All @@ -163,9 +160,8 @@ def handle_state_function_expected
@encoder.text_token(getch, :error)
@state = :initial
end
end

def handle_state_goto_label_expected
when :goto_label_expected
if match = scan(/[a-zA-Z_][a-zA-Z0-9_]*/)
@encoder.text_token(match, :label)
@state = :initial
Expand All @@ -174,9 +170,8 @@ def handle_state_goto_label_expected
else
@encoder.text_token(getch, :error)
end
end

def handle_state_local_var_expected

when :local_var_expected
if match = scan(/function/) # local function ...
@encoder.text_token(match, :keyword)
@state = :function_expected
Expand All @@ -198,9 +193,8 @@ def handle_state_local_var_expected
else
@encoder.text_token(getch, :error)
end
end

def handle_state_long_comment

when :long_comment
if match = scan(/.*?(?=\]={#@num_equals}\])/m)
@encoder.text_token(match, :content)

Expand All @@ -212,9 +206,8 @@ def handle_state_long_comment
end
@encoder.end_group(:comment)
@state = :initial
end

def handle_state_long_string

when :long_string
if match = scan(/.*?(?=\]={#@num_equals}\])/m) # Long strings do not interpret any escape sequences
@encoder.text_token(match, :content)

Expand All @@ -226,9 +219,8 @@ def handle_state_long_string
end
@encoder.end_group(:string)
@state = :initial
end

def handle_state_string

when :string
if match = scan(/[^\\#@start_delim\n]+/) # Everything except \ and the start delimiter character is string content (newlines are only allowed if preceeded by \ or \z)
@encoder.text_token(match, :content)
elsif match = scan(/\\(?:['"abfnrtv\\]|z\s*|x\h\h|\d{1,3}|\n)/m)
Expand All @@ -244,9 +236,8 @@ def handle_state_string
else
@encoder.text_token(getch, :error)
end
end

def handle_state_table

when :table
if match = scan(/[,;]/)
@encoder.text_token(match, :operator)
elsif match = scan(/[a-zA-Z_][a-zA-Z0-9_]* (?=\s*=)/x)
Expand All @@ -262,6 +253,13 @@ def handle_state_table
# advances the pointer).
@state = :initial
end
else
raise
end

end

@encoder
end

end
0