From a1159e94b68e346257dfe5528b7ae8ba5bd2334b Mon Sep 17 00:00:00 2001 From: Tan Le Date: Thu, 18 Aug 2022 19:59:20 +1000 Subject: [PATCH] Define missing namespace state for haxe lexer (#1858) * 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 --- lib/rouge/lexers/haxe.rb | 41 +++++++++++++++++++++++++++++----------- spec/visual/samples/haxe | 5 +++++ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/lib/rouge/lexers/haxe.rb b/lib/rouge/lexers/haxe.rb index df2c73b454..bca64ce88e 100644 --- a/lib/rouge/lexers/haxe.rb +++ b/lib/rouge/lexers/haxe.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/visual/samples/haxe b/spec/visual/samples/haxe index a377d6124b..f466eeae5f 100644 --- a/spec/visual/samples/haxe +++ b/spec/visual/samples/haxe @@ -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