Skip to content

Commit

Permalink
Define missing namespace state for haxe lexer (#1858)
Browse files Browse the repository at this point in the history
* Define missing namespace state for haxe lexer

This commit fixes an exception where namespace state is not defined.

- Standardise import, using and package statement
- Support import with alias
- Distinguish Keyword::Namespace token

* Support as keyword in import alias in Haxe
  • Loading branch information
tancnle committed Aug 18, 2022
1 parent 045d7bc commit a1159e9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
41 changes: 30 additions & 11 deletions lib/rouge/lexers/haxe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def self.detect?(text)

def self.keywords
@keywords ||= Set.new %w(
break case cast catch class continue default do else enum false for
function if import interface macro new null override package private
as break case cast catch class continue default do else enum false for
function if import in interface macro new null override package private
public return switch this throw true try untyped while
)
end

def self.imports
@imports ||= Set.new %w(
import using
package import using
)
end

Expand Down Expand Up @@ -55,6 +55,7 @@ def self.builtins
end

id = /[$a-zA-Z_][a-zA-Z0-9_]*/
dotted_id = /[$a-zA-Z_][a-zA-Z0-9_.]*/

state :comments_and_whitespace do
rule %r/\s+/, Text
Expand All @@ -77,6 +78,22 @@ def self.builtins
rule %r//, Text, :pop!
end

state :namespace do
mixin :comments_and_whitespace

rule %r/
(#{dotted_id})
(\s+)(in|as)(\s+)
(#{id})
/x do
groups(Name::Namespace, Text::Whitespace, Keyword, Text::Whitespace, Name)
end

rule %r/#{dotted_id}/, Name::Namespace

rule(//) { pop! }
end

state :regex do
rule %r(/) do
token Str::Regex
Expand Down Expand Up @@ -141,20 +158,22 @@ def self.builtins
end

rule id do |m|
if self.class.keywords.include? m[0]
match = m[0]

if self.class.imports.include?(match)
token Keyword::Namespace
push :namespace
elsif self.class.keywords.include?(match)
token Keyword
push :expr_start
elsif self.class.imports.include? m[0]
token Keyword
push :namespace
elsif self.class.declarations.include? m[0]
elsif self.class.declarations.include?(match)
token Keyword::Declaration
push :expr_start
elsif self.class.reserved.include? m[0]
elsif self.class.reserved.include?(match)
token Keyword::Reserved
elsif self.class.constants.include? m[0]
elsif self.class.constants.include?(match)
token Keyword::Constant
elsif self.class.builtins.include? m[0]
elsif self.class.builtins.include?(match)
token Name::Builtin
else
token Name::Other
Expand Down
5 changes: 5 additions & 0 deletions spec/visual/samples/haxe
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package a.b.c;

import haxe.macro.*;
import String.fromCharCode in f;
import String.fromCharCode as f;
using my.package.SomeStaticExtension;

// one line comment
/*
multiline
Expand Down

0 comments on commit a1159e9

Please sign in to comment.