Merge pull request #406 from chr15m/lib-alias-hacks
[jackhill/mal.git] / matlab / reader.m
index 624e30c..4fba3ed 100644 (file)
@@ -2,7 +2,7 @@
 classdef reader
     methods (Static = true)
         function tokens = tokenize(str)
-            re = '[\s,]*(~@|[\[\]{}()''`~^@]|"(?:\\.|[^\\"])*"|;[^\n]*|[^\s\[\]{}(''"`,;)]*)';
+            re = '[\s,]*(~@|[\[\]{}()''`~^@]|"(?:\\.|[^\\"])*"?|;[^\n]*|[^\s\[\]{}(''"`,;)]*)';
             % extract the capture group (to ignore spaces and commas)
             tokens = cellfun(@(x) x(1), regexp(str, re, 'tokens'));
             comments = cellfun(@(x) length(x) > 0 && x(1) == ';', tokens);
@@ -14,14 +14,19 @@ classdef reader
             %fprintf('in read_atom: %s\n', token);
             if not(isempty(regexp(token, '^-?[0-9]+$', 'match')))
                 atm = str2double(token);
-            elseif strcmp(token(1), '"')
+            elseif not(isempty(regexp(token, '^"(?:\\.|[^\\"])*"$', 'match')))
                 atm = token(2:length(token)-1);
+                atm = strrep(atm, '\\', char(255));
                 atm = strrep(atm, '\"', '"');
                 atm = strrep(atm, '\n', char(10));
+                atm = strrep(atm, char(255), '\');
+            elseif strcmp(token(1), '"')
+                error('expected ''"'', got EOF');
             elseif strcmp(token(1), ':')
-                atm = types.keyword(token(2:end));
+                s = token(2:end);
+                atm = type_utils.keyword(s);
             elseif strcmp(token, 'nil')
-                atm = types.nil;
+                atm = type_utils.nil;
             elseif strcmp(token, 'true')
                 atm = true;
             elseif strcmp(token, 'false')
@@ -36,12 +41,12 @@ classdef reader
             seq = {};
             token = rdr.next();
             if not(strcmp(token, start))
-                error(sprintf('expected ''%s''', start));
+                error(sprintf('expected ''%s'', got EOF', start));
             end
             token = rdr.peek();
             while true
                 if eq(token, false)
-                    error(sprintf('expected ''%s''', last));
+                    error(sprintf('expected ''%s'', got EOF', last));
                 end
                 if strcmp(token, last), break, end
                 seq{end+1} = reader.read_form(rdr);
@@ -115,6 +120,7 @@ classdef reader
         function ast = read_str(str)
             %fprintf('in read_str\n');
             tokens = reader.tokenize(str);
+            %disp(tokens);
             rdr = types.Reader(tokens);
             ast = reader.read_form(rdr);
         end