diff --git a/lib/rouge/demos/magik b/lib/rouge/demos/magik new file mode 100644 index 0000000000..80dfe262c9 --- /dev/null +++ b/lib/rouge/demos/magik @@ -0,0 +1,6 @@ +_private _method test_object.init() + ## initializer + .slot1 << _self.default_value + _return _self +_endmethod +$ diff --git a/lib/rouge/lexers/magik.rb b/lib/rouge/lexers/magik.rb new file mode 100644 index 0000000000..065de856ab --- /dev/null +++ b/lib/rouge/lexers/magik.rb @@ -0,0 +1,126 @@ +# -*- coding: utf-8 -*- # +# frozen_string_literal: true + +module Rouge + module Lexers + class Magik < RegexLexer + title "Magik" + desc "Smallworld Magik" + tag 'magik' + filenames '*.magik' + mimetypes 'text/x-magik', 'application/x-magik' + + def self.keywords + @keywords ||= %w( + _package + _pragma + _block _endblock + _handling _default + _protect _protection _endprotect + _try _with _when _endtry + _catch _endcatch + _throw + _lock _endlock + _if _then _elif _else _endif + _for _over _loop _endloop _loopbody _continue _leave + _return + _local _constant _recursive _global _dynamic _import + _private _iter _abstract _method _endmethod + _proc _endproc + _gather _scatter _allresults _optional + _thisthread _self _clone _super + _primitive + _unset _true _false _maybe + _is _isnt _not _and _or _xor _cf _andif _orif + _div _mod + ) + end + + def self.string_double + @string_double ||= /"[^"\n]*?"/ + end + def self.string_single + @string_single ||= /'[^'\n]*?'/ + end + + def self.digits + @digits ||= /[0-9]+/ + end + def self.radix + @radix ||= /r[0-9a-z]/i + end + def self.exponent + @exponent ||= /(e|&)[+-]?#{Magik.digits}/i + end + def self.decimal + @decimal ||= /\.#{Magik.digits}/ + end + def self.number + @number = /#{Magik.digits}(#{Magik.radix}|#{Magik.exponent}|#{Magik.decimal})*/ + end + + def self.character + @character ||= /%u[0-9a-z]{4}|%[^\s]+/i + end + + def self.simple_identifier + @simple_identifier ||= /(?>(?:[a-z0-9_!?]|\\.)+)/i + end + def self.piped_identifier + @piped_identifier ||= /\|[^\|\n]*\|/ + end + def self.identifier + @identifier ||= /(?:#{Magik.simple_identifier}|#{Magik.piped_identifier})+/i + end + def self.package_identifier + @package_identifier ||= /#{Magik.identifier}:#{Magik.identifier}/ + end + def self.symbol + @symbol ||= /:#{Magik.identifier}/i + end + def self.global_ref + @global_ref ||= /@[\s]*#{Magik.identifier}:#{Magik.identifier}/ + end + def self.label + @label = /@[\s]*#{Magik.identifier}/ + end + + state :root do + rule /##(.*)?/, Comment::Doc + rule /#(.*)?/, Comment::Single + + rule /(_method)(\s+)/ do + groups Keyword, Text::Whitespace + push :method_name + end + + rule /(?:#{Magik.keywords.join('|')})\b/, Keyword + + rule Magik.string_double, Literal::String + rule Magik.string_single, Literal::String + rule Magik.symbol, Str::Symbol + rule Magik.global_ref, Name::Label + rule Magik.label, Name::Label + rule Magik.character, Literal::String::Char + rule Magik.number, Literal::Number + rule Magik.package_identifier, Name + rule Magik.identifier, Name + + rule /[\[\]{}()\.,;]/, Punctuation + rule /\$/, Punctuation + rule /(<<|^<<)/, Operator + rule /(>>)/, Operator + rule /[-~+\/*%=&^<>]|!=/, Operator + + rule /[\s]+/, Text::Whitespace + end + + state :method_name do + rule /(#{Magik.identifier})(\.)(#{Magik.identifier})/ do + groups Name::Class, Punctuation, Name::Function + pop! + end + end + end + end +end diff --git a/spec/lexers/magik_spec.rb b/spec/lexers/magik_spec.rb new file mode 100644 index 0000000000..95f1bf9f09 --- /dev/null +++ b/spec/lexers/magik_spec.rb @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- # +# frozen_string_literal: true + +describe Rouge::Lexers::Magik do + let(:subject) { Rouge::Lexers::Magik.new } + + describe 'guessing' do + include Support::Guessing + + it 'guesses by filename' do + assert_guess :filename => 'foo.magik' + end + + it 'guesses by mimetype' do + assert_guess :mimetype => 'text/x-magik' + assert_guess :mimetype => 'application/x-magik' + end + end +end diff --git a/spec/visual/samples/magik b/spec/visual/samples/magik new file mode 100644 index 0000000000..d1bfcd4964 --- /dev/null +++ b/spec/visual/samples/magik @@ -0,0 +1,69 @@ +let_automatic_engine_start_unittests_for +abc| ab de |deffffffffffffffffffffffffffff +aaaaaaaaaaaaaaaaaa:bbbbbbbbbbbbbbbbbbbbbbbbb +@abcdefffffffffffffffffffffffffffffff +@abc:deffffffffffffffffffffffffff + +def_slotted_exemplar( + :test_object, + { + {:slot, _unset} + }) +$ + +_pragma(classify_level=basic, topic={test_topic, rouge}) +_method test_object.method_name(arg_1, _gather args) + _local x << _self.test() + 15 + _if arg_1 _is :test_symbol _orif + arg_1 <> "abc" _orif + arg_1 = "def" + _then + .slot << x + _else + .slot << write_string("string" + 'string', "test") + _endif + + _local l_y << _true + l_y _and<< _true + + # numbers + 45, 0, -101 + 2r101, -8r123 + 16rA0, 16rFF + 2e5, 6&6, 6rA0&2, 6rA0&2e4 + 0.5, 45.0, 16rA0.8 + 23.007e5, 0.4E-5, 16rA0.8&2 + + # characters + %q, %Z, %u + %u000A + %newline, %space, %tab + + # identifiers + identifier, identifier_abc, identifier_0123, |IDENTIFIER|, identifier:identifier + + # symbols + :symbol, :symbol_symbol, :|symbol()|, :|Symbol|, :|Symbol^<<__|, :\|test + + # labels + @label + @ label + + _proc@test(p_args) + _endproc + + _proc@ test(p_args) + _endproc + + # global references + @namespace:identifier + @123 + + >> l_x +_endmethod +$ + +_block + _thisthread.traceback() +_endblock +$