Skip to content

Commit 3600979

Browse files
committed
Merge pull request #423 from josephwilk/overzealous_builtin_check
Avoid false positives on builtin fns errors.
2 parents 23e164d + 4c7f7eb commit 3600979

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

app/server/sonicpi/lib/sonicpi/preparser.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ class PreParseError < StandardError ; end
2323
def self.preparse(rb)
2424
SonicPi::SpiderAPI.ring_fns.each do |fn|
2525
fn = fn[:name].to_s
26-
rb.gsub!(/\((\s*)#{fn}(\s)/, '\1' + ' ' + fn + '(\2')
27-
raise PreParseError, "You may not use the built-in fn names as variable names.\n You attempted to use: #{fn}" if rb.match(/\W#{fn}\s*=[\s\w]/)
26+
rb.gsub!(/\((\s*)#{fn}(\s)/, '\1' + fn + '(\2')
27+
28+
if rb.match(/(?!\B)\W?#{fn}\s*=[\s\w]/)
29+
raise PreParseError, "You may not use the built-in fn names as variable names.\n You attempted to use: #{fn}"
30+
end
2831
end
2932
rb
3033
end

app/server/sonicpi/test/test_preparser.rb

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@
1515
require_relative "../../core"
1616
require_relative "../lib/sonicpi/preparser"
1717

18-
module SonicPi
19-
module PreParser
20-
21-
class PreParseError < StandardError ; end
22-
23-
def self.preparse(rb)
24-
SonicPi::SpiderAPI.ring_fns.each do |fn|
25-
fn = fn[:name].to_s
26-
rb.gsub!(/\((\s*)#{fn}(\s)/, '\1' + fn + '(\2')
27-
raise PreParseError, "You may not use the built-in fn names as variable names.\n You attempted to use: #{fn}" if rb.match(/\W?#{fn}\s*=[\s\w]/)
28-
end
29-
rb
30-
end
31-
end
32-
end
33-
3418
module SonicPi
3519
class PreParserTester < Test::Unit::TestCase
3620
def test_no_change
@@ -50,5 +34,19 @@ def test_raises_on_assignment_to_ring_fn
5034
PreParser.preparse(a)
5135
end
5236
end
37+
38+
def test_partial_matches_on_builtin_fns
39+
a = "testscale = 10"
40+
assert_nothing_thrown PreParser::PreParseError do
41+
PreParser.preparse(a)
42+
end
43+
end
44+
45+
def test_using_a_builtin_raises_an_exception
46+
a = "scale = 10"
47+
assert_raise PreParser::PreParseError do
48+
PreParser.preparse(a)
49+
end
50+
end
5351
end
5452
end

0 commit comments

Comments
 (0)