Skip to content
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

Support for (Smallworld) Magik language #1044

Merged
merged 6 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
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
58 changes: 58 additions & 0 deletions lib/rouge/demos/magik
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
def_slotted_exemplar(
:test_object,
{
{:slot, _unset}
})
$

_method test_object.new()
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
_return _clone.init()
_endmethod
$

_method test_object.init()
## initializer
_return _self
_endmethod
$

_method test_object.run(arg_1, _gather args)
## Does something.
write("calling test_object.run()")
write(%tab, %newline)
show(:args, _scatter args)

_local x <<
_if arg_1 _is :start
_then
>> 10.0
_endif
_local results << rope.new()

_for arg _over rgs.fast_elements()
_loop
results.add_last(arg)
_endloop

_local stream << external_text_output_stream.new("$HOME/test.txt")
_protect
local y << p_args.map(_proc(p_obj)
>> p_obj + 1
_endproc
_protection
stream.close()
_endprotect

_return results
_endmethod
$

_block
_local obj << test_object.new()
_try _with cond
_local results << obj.run()
_when error
write("Caught error: ", cond.report_string)
_endtry
endblock
$
96 changes: 96 additions & 0 deletions lib/rouge/lexers/magik.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
module Lexers
class Magik < RegexLexer
title "Magik"
desc "Smallworld Magik"
tag 'magik'
aliases 'magik'
StevenLooman marked this conversation as resolved.
Show resolved Hide resolved
filenames '*.magik'
mimetypes 'text/x-magik', 'application/x-magik'

keywords = %w(
StevenLooman marked this conversation as resolved.
Show resolved Hide resolved
_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
)

string_double = /"[^"\n]*"/
StevenLooman marked this conversation as resolved.
Show resolved Hide resolved
string_single = /'[^'\n]*'/
StevenLooman marked this conversation as resolved.
Show resolved Hide resolved

digits = /[0-9]+/
StevenLooman marked this conversation as resolved.
Show resolved Hide resolved
hex_digits = /[0-9a-f]+/i
radix = /r#{hex_digits}/i
StevenLooman marked this conversation as resolved.
Show resolved Hide resolved
exponent = /(e|&)[+-]?#{digits}/i
decimal = /\.#{digits}/
number = /#{digits}(#{radix}|#{exponent}|#{decimal})*/

character = /%u[0-9a-z]{4}|%[^\s]+/i

simple_identifier = /(?>(?:[a-z0-9_!?]|\\.)+)/i
piped_identifier = /\|[^\|\n]*\|/
identifier = /(?:#{simple_identifier}|#{piped_identifier})+/i
package_identifier = /#{identifier}:#{identifier}/
symbol = /:#{identifier}/i
global_ref = /@[\s]*#{identifier}:#{identifier}/
label = /@[\s]*#{identifier}/

state :root do
rule /##(.*)?\n?/, Comment::Doc
rule /#(.*)?\n?/, Comment::Single
pyrmont marked this conversation as resolved.
Show resolved Hide resolved

rule /(_method)(\s+)/ do
groups Keyword, Text::Whitespace
push :method_name
end

rule /(?:#{keywords.join('|')})\b/, Keyword

rule string_double, Literal::String
rule string_single, Literal::String
rule symbol, Str::Symbol
rule global_ref, Name::Label
rule label, Name::Label
rule character, Literal::String::Char
rule number, Literal::Number
rule package_identifier, Name
rule identifier, Name

rule /[\[\]{}()\.,;]/, Punctuation
rule /\$/, Punctuation
rule /(<<|^<<)/, Operator
rule /(>>)/, Operator
rule /[-~+\/*%=&^<>]|!=/, Operator

rule /[\s]+/, Text::Whitespace
end

state :method_name do
rule /(#{identifier})(\.)(#{identifier})/ do
groups Name::Class, Punctuation, Name::Function
pop!
end
end
end
end
end
19 changes: 19 additions & 0 deletions spec/lexers/magik_spec.rb
Original file line number Diff line number Diff line change
@@ -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
69 changes: 69 additions & 0 deletions spec/visual/samples/magik
Original file line number Diff line number Diff line change
@@ -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
$