From affd866c96c27c7a7ba45ae42bdf8df1ccf6ba7d Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 15:25:26 -0400 Subject: [PATCH 01/20] add supercollider lexer spec --- spec/lexers/supercollider_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 spec/lexers/supercollider_spec.rb diff --git a/spec/lexers/supercollider_spec.rb b/spec/lexers/supercollider_spec.rb new file mode 100644 index 0000000000..931fd636ee --- /dev/null +++ b/spec/lexers/supercollider_spec.rb @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- # + +describe Rouge::Lexers::SuperCollider do + let(:subject) { Rouge::Lexers::SuperCollider.new } + + describe 'guessing' do + include Support::Guessing + + it 'guesses by filename' do + assert_guess :filename => 'foo.scd' + assert_guess :filename => 'foo.sc' + end + end +end From 019181e06db1b351b1f29290e74685f989d34c26 Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 15:25:56 -0400 Subject: [PATCH 02/20] add supercollider lexer visual sample --- spec/visual/samples/supercollider | 111 ++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 spec/visual/samples/supercollider diff --git a/spec/visual/samples/supercollider b/spec/visual/samples/supercollider new file mode 100644 index 0000000000..8127a7506e --- /dev/null +++ b/spec/visual/samples/supercollider @@ -0,0 +1,111 @@ +/* Test Class */ +Collection { + *newFrom { | aCollection | + var newCollection = this.new(aCollection.size); + aCollection.do {| item | newCollection.add(item) }; + ^newCollection + } + *with { | ... args | + var newColl; + // answer a collection of my class of the given arguments + // the class Array has a simpler implementation + newColl = this.new(args.size); + newColl.addAll(args); + ^newColl + } + *fill { | size, function | + var obj; + if(size.isSequenceableCollection) { ^this.fillND(size, function) }; + obj = this.new(size); + size.do { | i | + obj.add(function.value(i)); + }; + ^obj + } + *fill2D { | rows, cols, function | + var obj = this.new(rows); + rows.do { |row| + var obj2 = this.new(cols); + cols.do { |col| + obj2 = obj2.add(function.value(row, col)) + }; + obj = obj.add(obj2); + }; + ^obj + } + *fill3D { | planes, rows, cols, function | + var obj = this.new(planes); + planes.do { |plane| + var obj2 = this.new(rows); + rows.do { |row| + var obj3 = this.new(cols); + cols.do { |col| + obj3 = obj3.add(function.value(plane, row, col)) + }; + obj2 = obj2.add(obj3); + }; + obj = obj.add(obj2); + }; + ^obj + } +} + +/* This is a multiline comment + /* With nesting! */ + End of multiline comment */ + +~environmentVariable = { 3.do(_.postln) }; + +~environmentVariable.value(); + +~myFunction = { arg size, offset, freq; + postln("Bye!"); +}; + +// some numbers +// integers +0; +10; +33; +-235; +0x15; +0x159abcdef; +0x159ABCDEF; +2r01; +8r711; +36rabczyblkgh; +36rAZ19GH; + +// floats +0.0; +0.0e5; +1e5; +1e-5; +0.5e-5; +10r2034.5; +36r2358.abcdz; +36r2358.ABCDZ; +-36r2358.abcdz; +-36r2358.ABCDZ; +15pi; +15 pi; +-10 pi; +-10pi; +-1.5e7pi; + +// strings +"abcdefg hijklmnop"; +"\"quoted\""; +"\ttabbed\t"; +"\\\ttabbed with backslashes\t\\"; + +// characters +$a; +$ ; +$b; +$0; +$$; +$\t; +$\0; +$\&; +$\\; From 01cf2b680f701ff9792d09f60dce5334daf15434 Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 15:28:26 -0400 Subject: [PATCH 03/20] add supercollider demo file --- lib/rouge/demos/supercollider | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 lib/rouge/demos/supercollider diff --git a/lib/rouge/demos/supercollider b/lib/rouge/demos/supercollider new file mode 100644 index 0000000000..23a8e4b70b --- /dev/null +++ b/lib/rouge/demos/supercollider @@ -0,0 +1,11 @@ +// modulate a sine frequency and a noise amplitude with another sine +// whose frequency depends on the horizontal mouse pointer position +~myFunction = { + var x = SinOsc.ar(MouseX.kr(1, 100)); + SinOsc.ar(300 * x + 800, 0, 0.1) + + + PinkNoise.ar(0.1 * x + 0.1) +}; + +~myFunction.play; +"that's all, folks!".postln; From 73480835db8be3ed90c728e43fc597b0e30d146b Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 15:35:55 -0400 Subject: [PATCH 04/20] add (messy) supercollider lexer --- lib/rouge/lexers/supercollider.rb | 84 +++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 lib/rouge/lexers/supercollider.rb diff --git a/lib/rouge/lexers/supercollider.rb b/lib/rouge/lexers/supercollider.rb new file mode 100644 index 0000000000..3c7f358602 --- /dev/null +++ b/lib/rouge/lexers/supercollider.rb @@ -0,0 +1,84 @@ +# -*- coding: utf-8 -*- # + +module Rouge + module Lexers + class SuperCollider < RegexLexer + tag 'supercollider' + filenames '*.sc', '*.scd' + + title "SuperCollider" + desc 'A cross-platform interpreted programming language for sound synthesis, algorithmic composition, and realtime performance' + + keywords = Set.new %w( + case do for forBy loop if while + var arg classvar const super this + ) + + constants = Set.new %w( + true false nil inf thisThread + thisMethod thisFunction thisProcess + thisFunctionDef currentEnvironment + topEnvironment + ) + + start { push :bol } + + # beginning of line + state :bol do + mixin :inline_whitespace + + rule(//) { pop! } + end + + state :inline_whitespace do + rule /\s+/m, Text + mixin :has_comments + end + + state :whitespace do + rule /\n+/m, Text, :bol + rule %r(\/\/\.*?$), Comment::Single, :bol + mixin :inline_whitespace + end + + state :has_comments do + rule %r(/[*]), Comment::Multiline, :nested_comment + end + + state :nested_comment do + mixin :has_comments + rule %r([*]/), Comment::Multiline, :pop! + rule %r([^*/]+)m, Comment::Multiline + rule /./, Comment::Multiline + end + + state :root do + mixin :whitespace + + rule /$(\\.|.)/, Str::Char + rule /(\d+\*|\d*\.\d+)(e[+-]?[0-9]+)?/i, Num::Float + rule /\d+e[+-]?[0-9]+/i, Num::Float + rule /0x[0-9A-Fa-f]+/, Num::Hex + rule /0b[01]+/, Num::Bin + rule /\d+/, Num::Integer + +# from IDE's highlighter +# Token::RadixFloat, "^\\b\\d+r[0-9a-zA-Z]*(\\.[0-9A-Z]*)?" ); +# Token::Float, "^\\b((\\d+(\\.\\d+)?([eE][-+]?\\d+)?(pi)?)|pi)\\b" ); +# Token::HexInt, "^\\b0(x|X)(\\d|[a-f]|[A-F])+" ); +# Token::SymbolArg, "^\\b[A-Za-z_]\\w*\\:" ); +# Token::Name, "^[a-z]\\w*" ); +# Token::Class, "^\\b[A-Z]\\w*" ); +# Token::Primitive, "^\\b_\\w+" ); +# Token::Symbol, "^\\\\\\w*" ); +# Token::Char, "^\\$\\\\?." ); +# Token::EnvVar, "^~\\w+" ); +# Token::SingleLineComment, "^//[^\r\n]*" ); +# Token::MultiLineCommentStart, "^/\\*" ); +# Token::Operator, "^[\\+-\\*/&\\|\\^%<>=]+" ); + + end + end + end +end + From c0a8fc7b018c45d43fc65c4b17d0f978a7ef0ebb Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 16:39:30 -0400 Subject: [PATCH 05/20] supercollider: add material to visual sample --- spec/visual/samples/supercollider | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/spec/visual/samples/supercollider b/spec/visual/samples/supercollider index 8127a7506e..9082e964e0 100644 --- a/spec/visual/samples/supercollider +++ b/spec/visual/samples/supercollider @@ -1,3 +1,15 @@ +// modulate a sine frequency and a noise amplitude with another sine +// whose frequency depends on the horizontal mouse pointer position +~myFunction = { + var x = SinOsc.ar(MouseX.kr(1, 100)); + SinOsc.ar(300 * x + 800, 0, 0.1) + + + PinkNoise.ar(0.1 * x + 0.1) +}; + +~myFunction.play; +"that's all, folks!".postln; + /* Test Class */ Collection { *newFrom { | aCollection | @@ -62,6 +74,13 @@ Collection { postln("Bye!"); }; +// function calls with symbol arguments: +s.boot(startAliveThread: false, recover: true); +Array.geom(size: 5, start: 1, grow: 8); +[ 1, 8, 64, 512, 4096 ].collect { arg n; + n.squared +}; + // some numbers // integers 0; @@ -83,9 +102,7 @@ Collection { 1e-5; 0.5e-5; 10r2034.5; -36r2358.abcdz; 36r2358.ABCDZ; --36r2358.abcdz; -36r2358.ABCDZ; 15pi; 15 pi; @@ -99,6 +116,13 @@ Collection { "\ttabbed\t"; "\\\ttabbed with backslashes\t\\"; +// symbols +\abc +\a1 +\a_symbol +'a symbol' +'a cl3v3r\'_symb0l"' + // characters $a; $ ; From a4731820ed2cfb92ed81904b19e1a7d747b76c85 Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 16:40:10 -0400 Subject: [PATCH 06/20] supercollider lexer: create new reserved words category --- lib/rouge/lexers/supercollider.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/rouge/lexers/supercollider.rb b/lib/rouge/lexers/supercollider.rb index 3c7f358602..8fbc39cab4 100644 --- a/lib/rouge/lexers/supercollider.rb +++ b/lib/rouge/lexers/supercollider.rb @@ -10,10 +10,16 @@ class SuperCollider < RegexLexer desc 'A cross-platform interpreted programming language for sound synthesis, algorithmic composition, and realtime performance' keywords = Set.new %w( - case do for forBy loop if while var arg classvar const super this ) + # these aren't technically keywords, but we treat + # them as such because it makes things clearer 99% + # of the time + reserved = Set.new %w( + case do for forBy loop if while new newCopyArgs + ) + constants = Set.new %w( true false nil inf thisThread thisMethod thisFunction thisProcess From d0758d586df3e6151fe8e1122529a92f688e38b0 Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 16:40:18 -0400 Subject: [PATCH 07/20] supercollider lexer: flesh out rules --- lib/rouge/lexers/supercollider.rb | 97 ++++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 21 deletions(-) diff --git a/lib/rouge/lexers/supercollider.rb b/lib/rouge/lexers/supercollider.rb index 8fbc39cab4..8223a3a09d 100644 --- a/lib/rouge/lexers/supercollider.rb +++ b/lib/rouge/lexers/supercollider.rb @@ -61,27 +61,82 @@ class SuperCollider < RegexLexer state :root do mixin :whitespace - rule /$(\\.|.)/, Str::Char - rule /(\d+\*|\d*\.\d+)(e[+-]?[0-9]+)?/i, Num::Float - rule /\d+e[+-]?[0-9]+/i, Num::Float - rule /0x[0-9A-Fa-f]+/, Num::Hex - rule /0b[01]+/, Num::Bin - rule /\d+/, Num::Integer - -# from IDE's highlighter -# Token::RadixFloat, "^\\b\\d+r[0-9a-zA-Z]*(\\.[0-9A-Z]*)?" ); -# Token::Float, "^\\b((\\d+(\\.\\d+)?([eE][-+]?\\d+)?(pi)?)|pi)\\b" ); -# Token::HexInt, "^\\b0(x|X)(\\d|[a-f]|[A-F])+" ); -# Token::SymbolArg, "^\\b[A-Za-z_]\\w*\\:" ); -# Token::Name, "^[a-z]\\w*" ); -# Token::Class, "^\\b[A-Z]\\w*" ); -# Token::Primitive, "^\\b_\\w+" ); -# Token::Symbol, "^\\\\\\w*" ); -# Token::Char, "^\\$\\\\?." ); -# Token::EnvVar, "^~\\w+" ); -# Token::SingleLineComment, "^//[^\r\n]*" ); -# Token::MultiLineCommentStart, "^/\\*" ); -# Token::Operator, "^[\\+-\\*/&\\|\\^%<>=]+" ); + #################### + ##### LITERALS ##### + #################### + + # hex number + rule /[\-+]?0[xX]\h+/, Num::Hex + + # radix float + rule /[\-+]?\d+r[0-9a-zA-Z]*(\.[0-9A-Z]*)?/, Num::Float + + # normal float + rule /[\-+]?((\d+(\.\d+)?([eE][\-+]?\d+)?(pi)?)|pi)/, Num::Float + + # integer + rule /[\-+]?\d+/, Num::Integer + + # character + rule /\$(\\.|.)/, Str::Char + + # strings + rule /"([^\\"]|\\.)*"/, Str + + # symbols (single-quote notation) + rule /'([^\\']|\\.)*'/, Str::Other + + # symbols (backslash notation) + rule /\\\w+/, Str::Other + + #################### + ##### COMMENTS ##### + #################### + + # single-line comments + rule /\/\/.*$/, Comment::Single + + ####################### + ##### IDENTIFIERS ##### + ####################### + + # symbol arg + rule /[A-Za-z_]\w*:/, Name::Label + + # class name + rule /[A-Z]\w*/, Name::Class + rule /_\W+/, Name::Function + + # main identifiers section + rule /[a-z]\w*/ do |m| + if keywords.include? m[0] + token Keyword + elsif constants.include? m[0] + token Keyword::Constant + elsif reserved.include? m[0] + token Keyword::Reserved + else + token Name + end + end + + # environment variables + rule /~\w+/, Name::Variable::Global + + # symbol arg + + ################# + ##### OTHER ##### + ################# + + # punctuation + rule /[\{\}()\[\];,\.]/, Punctuation + + # operators + rule /[\+\-\*\/&\|%<>=]+/, Operator + + # treat "return" as a special operator + rule /\^/, Keyword end end From 810d27cbf30cf25dbe554d4bed3835d903046cb2 Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 16:46:40 -0400 Subject: [PATCH 08/20] supercollider lexer: fix rules for primitives and operators --- lib/rouge/lexers/supercollider.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/rouge/lexers/supercollider.rb b/lib/rouge/lexers/supercollider.rb index 8223a3a09d..9723a1e401 100644 --- a/lib/rouge/lexers/supercollider.rb +++ b/lib/rouge/lexers/supercollider.rb @@ -105,7 +105,9 @@ class SuperCollider < RegexLexer # class name rule /[A-Z]\w*/, Name::Class - rule /_\W+/, Name::Function + + # primitive + rule /_\w+/, Name::Function # main identifiers section rule /[a-z]\w*/ do |m| @@ -134,9 +136,10 @@ class SuperCollider < RegexLexer # operators rule /[\+\-\*\/&\|%<>=]+/, Operator + rule /[\^:]/, Operator - # treat "return" as a special operator - rule /\^/, Keyword + # treat curry argument as a special operator + rule /_/, Name::Builtin end end From 6348e7bd805f7e86897f1e297e5e3955b96f56e3 Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 16:48:16 -0400 Subject: [PATCH 09/20] supercollider lexer: add more test code to visual sample --- spec/visual/samples/supercollider | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/spec/visual/samples/supercollider b/spec/visual/samples/supercollider index 9082e964e0..5a84bc7c18 100644 --- a/spec/visual/samples/supercollider +++ b/spec/visual/samples/supercollider @@ -60,6 +60,19 @@ Collection { }; ^obj } + + // from Array + mirror2 { + _ArrayMirror2 + ^this.primitiveFailed + } +} + +// class inheritance +MyClass : Object { + classvar <>decorations; + var <>igloos; + const magicNumber = 3; } /* This is a multiline comment @@ -71,7 +84,7 @@ Collection { ~environmentVariable.value(); ~myFunction = { arg size, offset, freq; - postln("Bye!"); + postln("Bye!"); }; // function calls with symbol arguments: From d9e11e57070a85726188778719c2f81837df8727 Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 17:00:24 -0400 Subject: [PATCH 10/20] supercollider visual sample: fix indent --- spec/visual/samples/supercollider | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/visual/samples/supercollider b/spec/visual/samples/supercollider index 5a84bc7c18..aa8af24e08 100644 --- a/spec/visual/samples/supercollider +++ b/spec/visual/samples/supercollider @@ -61,8 +61,8 @@ Collection { ^obj } - // from Array - mirror2 { + // from Array + mirror2 { _ArrayMirror2 ^this.primitiveFailed } From 3a2b7f57fb56babc080d801fb27970199342b51b Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 19:16:41 -0400 Subject: [PATCH 11/20] supercollider lexer: treat lib/rouge/lexers/supercollider.rb as an operator for array unpacking --- lib/rouge/lexers/supercollider.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rouge/lexers/supercollider.rb b/lib/rouge/lexers/supercollider.rb index 9723a1e401..39ed7b227d 100644 --- a/lib/rouge/lexers/supercollider.rb +++ b/lib/rouge/lexers/supercollider.rb @@ -134,9 +134,9 @@ class SuperCollider < RegexLexer # punctuation rule /[\{\}()\[\];,\.]/, Punctuation - # operators + # operators. treat # (array unpack) as an operator rule /[\+\-\*\/&\|%<>=]+/, Operator - rule /[\^:]/, Operator + rule /[\^:#]/, Operator # treat curry argument as a special operator rule /_/, Name::Builtin From e1df6afa2ae23fcc2e1878b2ab3abe26a978a801 Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 19:21:02 -0400 Subject: [PATCH 12/20] supercollider lexer: add array unpacking to visual sample --- spec/visual/samples/supercollider | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/visual/samples/supercollider b/spec/visual/samples/supercollider index aa8af24e08..2a1e7ff172 100644 --- a/spec/visual/samples/supercollider +++ b/spec/visual/samples/supercollider @@ -94,6 +94,9 @@ Array.geom(size: 5, start: 1, grow: 8); n.squared }; +// array unpacking +#a, b, c = [1, 4, 9]; + // some numbers // integers 0; From 7050e28cfa3c7d0505b098427ff6478165889888 Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Mon, 14 Aug 2017 20:47:53 -0400 Subject: [PATCH 13/20] python/supercollider: give *.sc extension to supercollider --- lib/rouge/lexers/python.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rouge/lexers/python.rb b/lib/rouge/lexers/python.rb index 8225a26364..ccfda1de60 100644 --- a/lib/rouge/lexers/python.rb +++ b/lib/rouge/lexers/python.rb @@ -8,7 +8,7 @@ class Python < RegexLexer desc "The Python programming language (python.org)" tag 'python' aliases 'py' - filenames '*.py', '*.pyw', '*.sc', 'SConstruct', 'SConscript', '*.tac' + filenames '*.py', '*.pyw', 'SConstruct', 'SConscript', '*.tac' mimetypes 'text/x-python', 'application/x-python' def self.detect?(text) From b3175f0a720e1e62a21f454b3617979a4e2156cf Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Wed, 16 Jan 2019 21:16:29 -0600 Subject: [PATCH 14/20] comments --- lib/rouge/lexers/supercollider.rb | 26 +------------------------- spec/lexers/supercollider_spec.rb | 1 + 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/lib/rouge/lexers/supercollider.rb b/lib/rouge/lexers/supercollider.rb index 39ed7b227d..5025e5af60 100644 --- a/lib/rouge/lexers/supercollider.rb +++ b/lib/rouge/lexers/supercollider.rb @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- # +# frozen_string_literal: true module Rouge module Lexers @@ -61,11 +62,6 @@ class SuperCollider < RegexLexer state :root do mixin :whitespace - #################### - ##### LITERALS ##### - #################### - - # hex number rule /[\-+]?0[xX]\h+/, Num::Hex # radix float @@ -74,13 +70,10 @@ class SuperCollider < RegexLexer # normal float rule /[\-+]?((\d+(\.\d+)?([eE][\-+]?\d+)?(pi)?)|pi)/, Num::Float - # integer rule /[\-+]?\d+/, Num::Integer - # character rule /\$(\\.|.)/, Str::Char - # strings rule /"([^\\"]|\\.)*"/, Str # symbols (single-quote notation) @@ -89,21 +82,11 @@ class SuperCollider < RegexLexer # symbols (backslash notation) rule /\\\w+/, Str::Other - #################### - ##### COMMENTS ##### - #################### - - # single-line comments rule /\/\/.*$/, Comment::Single - ####################### - ##### IDENTIFIERS ##### - ####################### - # symbol arg rule /[A-Za-z_]\w*:/, Name::Label - # class name rule /[A-Z]\w*/, Name::Class # primitive @@ -125,13 +108,6 @@ class SuperCollider < RegexLexer # environment variables rule /~\w+/, Name::Variable::Global - # symbol arg - - ################# - ##### OTHER ##### - ################# - - # punctuation rule /[\{\}()\[\];,\.]/, Punctuation # operators. treat # (array unpack) as an operator diff --git a/spec/lexers/supercollider_spec.rb b/spec/lexers/supercollider_spec.rb index 931fd636ee..0b1447ca56 100644 --- a/spec/lexers/supercollider_spec.rb +++ b/spec/lexers/supercollider_spec.rb @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- # +# frozen_string_literal: true describe Rouge::Lexers::SuperCollider do let(:subject) { Rouge::Lexers::SuperCollider.new } From 98c8f9f2779e5a237f0aa1e2b1edf991a4f87f5b Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Tue, 4 Jun 2019 13:07:03 +0900 Subject: [PATCH 15/20] Add disambiguation for SCons files --- lib/rouge/guessers/disambiguation.rb | 7 +++++++ lib/rouge/lexers/python.rb | 2 +- spec/lexers/python_spec.rb | 4 ++++ spec/lexers/supercollider_spec.rb | 3 ++- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/rouge/guessers/disambiguation.rb b/lib/rouge/guessers/disambiguation.rb index 0bf14bb234..4767291a3f 100644 --- a/lib/rouge/guessers/disambiguation.rb +++ b/lib/rouge/guessers/disambiguation.rb @@ -107,6 +107,13 @@ def match?(filename) Plist end + + disambiguate '*.sc' do + next Python if matches?(/^#/) + next SuperCollider if matches?(/(?:^~|;$)/) + + next Python + end end end end diff --git a/lib/rouge/lexers/python.rb b/lib/rouge/lexers/python.rb index ccfda1de60..8225a26364 100644 --- a/lib/rouge/lexers/python.rb +++ b/lib/rouge/lexers/python.rb @@ -8,7 +8,7 @@ class Python < RegexLexer desc "The Python programming language (python.org)" tag 'python' aliases 'py' - filenames '*.py', '*.pyw', 'SConstruct', 'SConscript', '*.tac' + filenames '*.py', '*.pyw', '*.sc', 'SConstruct', 'SConscript', '*.tac' mimetypes 'text/x-python', 'application/x-python' def self.detect?(text) diff --git a/spec/lexers/python_spec.rb b/spec/lexers/python_spec.rb index 9dd0b65361..de0139754d 100644 --- a/spec/lexers/python_spec.rb +++ b/spec/lexers/python_spec.rb @@ -10,6 +10,10 @@ it 'guesses by filename' do assert_guess :filename => 'foo.py' assert_guess :filename => 'foo.pyw' + assert_guess :filename => '*.sc', :source => '# A comment' + assert_guess :filename => 'SConstruct' + assert_guess :filename => 'SConscript' + assert_guess :filename => 'foo.tac' end it 'guesses by mimetype' do diff --git a/spec/lexers/supercollider_spec.rb b/spec/lexers/supercollider_spec.rb index 0b1447ca56..a1bd9dc5aa 100644 --- a/spec/lexers/supercollider_spec.rb +++ b/spec/lexers/supercollider_spec.rb @@ -9,7 +9,8 @@ it 'guesses by filename' do assert_guess :filename => 'foo.scd' - assert_guess :filename => 'foo.sc' + assert_guess :filename => 'foo.sc', :source => '~x = 3' + assert_guess :filename => 'foo.sc', :source => '0;' end end end From eec82cd4ce0d559a6f72fcfd7bde82065556b70d Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Tue, 4 Jun 2019 20:35:04 -0500 Subject: [PATCH 16/20] supercollider: use defs rather than local vars see https://github.com/rouge-ruby/rouge/pull/749#discussion_r290102896 --- lib/rouge/lexers/supercollider.rb | 36 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/rouge/lexers/supercollider.rb b/lib/rouge/lexers/supercollider.rb index 5025e5af60..59079b16ec 100644 --- a/lib/rouge/lexers/supercollider.rb +++ b/lib/rouge/lexers/supercollider.rb @@ -10,23 +10,29 @@ class SuperCollider < RegexLexer title "SuperCollider" desc 'A cross-platform interpreted programming language for sound synthesis, algorithmic composition, and realtime performance' - keywords = Set.new %w( - var arg classvar const super this - ) + def self.keywords + @keywords ||= Set.new %w( + var arg classvar const super this + ) + end # these aren't technically keywords, but we treat # them as such because it makes things clearer 99% # of the time - reserved = Set.new %w( - case do for forBy loop if while new newCopyArgs - ) + def self.reserved + @reserved ||= Set.new %w( + case do for forBy loop if while new newCopyArgs + ) + end - constants = Set.new %w( - true false nil inf thisThread - thisMethod thisFunction thisProcess - thisFunctionDef currentEnvironment - topEnvironment - ) + def self.constants + @constants ||= Set.new %w( + true false nil inf thisThread + thisMethod thisFunction thisProcess + thisFunctionDef currentEnvironment + topEnvironment + ) + end start { push :bol } @@ -94,11 +100,11 @@ class SuperCollider < RegexLexer # main identifiers section rule /[a-z]\w*/ do |m| - if keywords.include? m[0] + if self.class.keywords.include? m[0] token Keyword - elsif constants.include? m[0] + elsif self.class.constants.include? m[0] token Keyword::Constant - elsif reserved.include? m[0] + elsif self.class.reserved.include? m[0] token Keyword::Reserved else token Name From 2a335a1aef58eb3de236fbb828374637ea0287eb Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Tue, 4 Jun 2019 20:39:42 -0500 Subject: [PATCH 17/20] supercollider: update visual sample --- spec/visual/samples/supercollider | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/visual/samples/supercollider b/spec/visual/samples/supercollider index 2a1e7ff172..494a6abb6f 100644 --- a/spec/visual/samples/supercollider +++ b/spec/visual/samples/supercollider @@ -133,11 +133,11 @@ Array.geom(size: 5, start: 1, grow: 8); "\\\ttabbed with backslashes\t\\"; // symbols -\abc -\a1 -\a_symbol -'a symbol' -'a cl3v3r\'_symb0l"' +\abc; +\a1; +\a_symbol; +'a symbol'; +'a cl3v3r\'_symb0l"'; // characters $a; From f509b8e6759e3d9db11872754daa1102de5df85e Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Wed, 5 Jun 2019 14:35:32 +0900 Subject: [PATCH 18/20] Refactor lexer --- lib/rouge/lexers/supercollider.rb | 33 +++++++++---------------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/lib/rouge/lexers/supercollider.rb b/lib/rouge/lexers/supercollider.rb index 59079b16ec..d0b3f3686f 100644 --- a/lib/rouge/lexers/supercollider.rb +++ b/lib/rouge/lexers/supercollider.rb @@ -34,39 +34,27 @@ def self.constants ) end - start { push :bol } - - # beginning of line - state :bol do - mixin :inline_whitespace - - rule(//) { pop! } - end - - state :inline_whitespace do - rule /\s+/m, Text - mixin :has_comments - end - state :whitespace do - rule /\n+/m, Text, :bol - rule %r(\/\/\.*?$), Comment::Single, :bol - mixin :inline_whitespace + rule /\s+/m, Text end - state :has_comments do - rule %r(/[*]), Comment::Multiline, :nested_comment + state :comments do + rule %r(//.*?$), Comment::Single + rule %r(/[*]) do + token Comment::Multiline + push :nested_comment + end end state :nested_comment do - mixin :has_comments + rule %r(/[*]), Comment::Multiline, :nested_comment rule %r([*]/), Comment::Multiline, :pop! rule %r([^*/]+)m, Comment::Multiline - rule /./, Comment::Multiline end state :root do mixin :whitespace + mixin :comments rule /[\-+]?0[xX]\h+/, Num::Hex @@ -88,8 +76,6 @@ def self.constants # symbols (backslash notation) rule /\\\w+/, Str::Other - rule /\/\/.*$/, Comment::Single - # symbol arg rule /[A-Za-z_]\w*:/, Name::Label @@ -122,7 +108,6 @@ def self.constants # treat curry argument as a special operator rule /_/, Name::Builtin - end end end From badb24f9cf18a95c79cc29f700f33076546977e6 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Wed, 5 Jun 2019 15:15:51 +0900 Subject: [PATCH 19/20] Correct bugs in lexer --- lib/rouge/lexers/supercollider.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rouge/lexers/supercollider.rb b/lib/rouge/lexers/supercollider.rb index d0b3f3686f..9a13610c4c 100644 --- a/lib/rouge/lexers/supercollider.rb +++ b/lib/rouge/lexers/supercollider.rb @@ -50,6 +50,7 @@ def self.constants rule %r(/[*]), Comment::Multiline, :nested_comment rule %r([*]/), Comment::Multiline, :pop! rule %r([^*/]+)m, Comment::Multiline + rule /./, Comment::Multiline end state :root do @@ -107,7 +108,7 @@ def self.constants rule /[\^:#]/, Operator # treat curry argument as a special operator - rule /_/, Name::Builtin + rule /\b_\b/, Name::Builtin end end end From 77625d6d30ad846567363c539a3ae38c8b878744 Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Wed, 5 Jun 2019 19:50:14 -0500 Subject: [PATCH 20/20] supercollider: add visual sample tests for comments and curry args --- spec/visual/samples/supercollider | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/visual/samples/supercollider b/spec/visual/samples/supercollider index 494a6abb6f..fd3c29beab 100644 --- a/spec/visual/samples/supercollider +++ b/spec/visual/samples/supercollider @@ -149,3 +149,18 @@ $\t; $\0; $\&; $\\; + +// curry argument vs underscore in name +[1, 2, 3].do(_.postln); +[1, 2, 3].collect(_ + _); +variable_name.function_name(Class_Name); + +// comments +/* abc */ notComment; +/* /* */ comment */ notComment; +/* /* * */ comment */ notComment; +/* /*/ comment */ comment */ notComment; +/* /**/ comment */ notComment; +// /* +notComment; +// */