]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
Fixed all pending specs
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 17 Oct 2014 15:06:38 +0000 (11:06 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 17 Oct 2014 15:06:38 +0000 (11:06 -0400)
spec/lexer_spec.rb
spec/parser_spec.rb
spec/spec_helper.rb

index 10c822f901986ecf1b5396a7d304cfe5fd8edf87..7857634ef4da1b9958e378a9eddf64ce2c26fd8f 100644 (file)
@@ -1,11 +1,4 @@
-require 'open3'
-
-def lexer(input)
-  out, err, status = Open3.capture3('./build/bin/sclpl-test', '--tokens', :stdin_data => input)
-  raise "Lexer command returned non-zero status" unless status.success?
-  raise err unless err == ""
-  out.gsub(/<tok (T_[A-Z]+)>/,'\1').split
-end
+require 'spec_helper'
 
 describe "lexer" do
   context "punctuation" do
@@ -156,7 +149,6 @@ describe "lexer" do
     end
 
     it "should recognize a string that spans lines" do
-      pending "S-Expression parser is stupid. fix it."
       expect(lexer("\"a\nb\"")).to eq ["T_STRING:\"a\nb\""]
     end
   end
index 858c1db734ae367e4334eeb175d58e122574c37a..dda0c4d2575c078753c5d49eafe4eca120939cca 100644 (file)
@@ -1,31 +1,5 @@
 require 'open3'
 
-def re_structure( token_array, offset = 0 )
-  struct = []
-  while( offset < token_array.length )
-    if(token_array[offset] == "(")
-      # Multiple assignment from the array that re_structure() returns
-      offset, tmp_array = re_structure(token_array, offset + 1)
-      struct << tmp_array
-    elsif(token_array[offset] == ")")
-      break
-    else
-      struct << token_array[offset]
-    end
-    offset += 1
-  end
-  return [offset, struct]
-end
-
-def ast(input)
-  out, err, status = Open3.capture3('./build/bin/sclpl-test', '--ast', :stdin_data => input)
-  raise err unless err == ""
-  raise "Parser command returned non-zero status" unless status.success?
-  out.gsub!(/([()])|tree/,' \1 ')
-  off, expr = re_structure(out.split)
-  expr
-end
-
 describe "sclpl grammar" do
   context "requires" do
     it "should parse a require statement" do
index ce150c689a6d3fbece4924fbce0b9095474d7d92..521a46380a77e44c5757a76c2a406820c25f02ef 100644 (file)
@@ -1,7 +1,51 @@
+require 'open3'
 
-module TestUtils
-def self.exec_cmd(cmd, input)
-  out, err, status = Open3.capture3(*cmd, :stdin_data => input)
+def lexer(input)
+  out, err, status = Open3.capture3('./build/bin/sclpl-test', '--tokens', :stdin_data => input)
+  raise "Lexer command returned non-zero status" unless status.success?
+  raise err unless err == ""
+  out.scan(/^(T_[A-Z]+(:("[^"]*"|[^\n]+))?)/m).map {|m| m[0] }
 end
+
+def re_structure( token_array, offset = 0 )
+  struct = []
+  while( offset < token_array.length )
+    if(token_array[offset] == "(")
+      # Multiple assignment from the array that re_structure() returns
+      offset, tmp_array = re_structure(token_array, offset + 1)
+      struct << tmp_array
+    elsif(token_array[offset] == ")")
+      break
+    else
+      struct << token_array[offset]
+    end
+    offset += 1
+  end
+  return [offset, struct]
+end
+
+def ast(input)
+  out, err, status = Open3.capture3('./build/bin/sclpl-test', '--ast', :stdin_data => input)
+  raise err unless err == ""
+  raise "Parser command returned non-zero status" unless status.success?
+  # Prep the parens for reading
+  out.gsub!(/([()])|tree/,' \1 ')
+  # Replace string literals so we can tokenize on spaces
+  strings = []
+  out.gsub!(/"[^\"]*"/) do |m|
+      strings << m
+      '$_$_STRING_$_$'
+  end
+  # Put the strings back in after splitting
+  tokens = out.split.map do |tok|
+    if tok.end_with? '$_$_STRING_$_$'
+      tok.gsub('$_$_STRING_$_$', strings.shift)
+    else
+      tok
+    end
+  end
+  # Build the tree structure and return the result
+  off, expr = re_structure(tokens)
+  expr
 end