[SCM] Lisaac compiler branch, master, updated. lisaac-0.12-653-gb404e41

Mildred Ki'Lya silkensedai at online.fr
Sat Sep 4 15:03:48 UTC 2010


The following commit has been merged in the master branch:
commit b404e4101efaf91b80d77722d20210aba4139e13
Author: Mildred Ki'Lya <silkensedai at online.fr>
Date:   Sat Sep 5 16:51:32 2009 +0200

    Writing string constants is externalized in backends
    
    string escape characters are parsed in PARSER and the escape characters are
    regenerated again when written to the source file. Implementation is in
    BACKEND

diff --git a/src/constant/character_cst.li b/src/constant/character_cst.li
index 63f0f3a..1e52505 100644
--- a/src/constant/character_cst.li
+++ b/src/constant/character_cst.li
@@ -38,20 +38,20 @@ Section Public
   // Value.
   //
 
-  + text:STRING_CONSTANT;
+  + text:CHARACTER;
 
   //
   // Creation.
   //
 
-  - create p:POSITION char car:STRING_CONSTANT :SELF<-
+  - create p:POSITION char car:CHARACTER :SELF<-
   ( + result:SELF;
     result := clone;
     result.make p char car;
     result
   );
 
-  - make p:POSITION char car:STRING_CONSTANT <-
+  - make p:POSITION char car:CHARACTER <-
   (
     position := p;
     text := car;
@@ -74,24 +74,13 @@ Section Public
   // Generation.
   //
 
-  - genere buffer:STRING <-
-  (
-    buffer.add_last '\'';
-    buffer.append text;
-    buffer.add_last '\'';
-  );
+  - genere buffer:STRING <- backend.append_character text in buffer;
 
   //
   // Display.
   //
 
-  - display buffer:STRING <-
-  (
-    buffer.add_last '\'';
-    buffer.append text;
-    buffer.add_last '\'';
-    display_ref buffer;
-  );
+  - display buffer:STRING <- BACKEND_C.append_character text in buffer;
 
 
 
diff --git a/src/constant/native_array_character_cst.li b/src/constant/native_array_character_cst.li
index 9b5646c..a9a1e37 100644
--- a/src/constant/native_array_character_cst.li
+++ b/src/constant/native_array_character_cst.li
@@ -74,25 +74,13 @@ Section Public
   // Generation.
   //
 
-  - genere buffer:STRING <-
-  (
-    buffer.add_last '\"';
-    buffer.append string;
-    buffer.add_last '\"';
-  );
+  - genere buffer:STRING <- backend.append_string string in buffer;
 
   //
   // Display.
   //
 
-  - display buffer:STRING <-
-  (
-    buffer.add_last '\"';
-    buffer.append string;
-    buffer.add_last '\"';
-    display_ref buffer;
-  );
-
+  - display buffer:STRING <- BACKEND_C.append_string string in buffer;
 
 
 
diff --git a/src/constant/string_cst.li b/src/constant/string_cst.li
index 751f27e..3c8b1c4 100644
--- a/src/constant/string_cst.li
+++ b/src/constant/string_cst.li
@@ -87,6 +87,9 @@ Section Public
 
   - genere buffer:STRING <-
   ( + idx,count,cur:INTEGER;
+//     + count_esc:INTEGER;
+//     + esc_octal:BOOLEAN;
+//     + esc_hexa :BOOLEAN;
     - is_init:BOOLEAN;
     - is_storage:BOOLEAN;
     - is_count:BOOLEAN;
@@ -110,7 +113,13 @@ Section Public
       };
       output.append "__";
       output.append (type_string_constant.intern_name);
-      output.append " __string_";
+      ((output.count - cur) >= 78).if {
+        output.add_last '\n';
+        cur := output.count - 1;
+      } else {
+        output.add_last ' ';
+      };
+      output.append "__string_";
       idx.append_in output;
       output.add_last '=';
       (is_java).if {
@@ -128,12 +137,20 @@ Section Public
         output.add_last ',';
       };
       (is_storage).if {
+        ((output.count - cur) >= 78).if {
+          output.add_last '\n';
+          cur := output.count - 1;
+        };
 	output.add_last '\"';
-	output.append string;
-	{(output.count - cur) > 78}.while_do {
-	  output.insert_string "\\\n" to (cur+78);
-	  cur := cur + 78;
-	};
+//         output.append string;
+//         {(output.count - cur) > 78}.while_do {
+//           output.insert_string "\\\n" to (cur+78);
+//           cur := cur + 78;
+//         };
+        backend.append_escaped_string string
+                in                    output
+                split_every           78
+                starting              (output.count - cur);
 	output.add_last '\"';
       } else {
         output.remove_last 1;
@@ -162,13 +179,8 @@ Section Public
   // Display.
   //
 
-  - display buffer:STRING <-
-  (
-    buffer.add_last '\"';
-    buffer.append string;
-    buffer.add_last '\"';
-    display_ref buffer;
-  );
+  - display buffer:STRING <- BACKEND_C.append_string string in buffer;
+
 
 Section Private
 
diff --git a/src/item/itm_character.li b/src/item/itm_character.li
index 9c6c314..ef0bf17 100644
--- a/src/item/itm_character.li
+++ b/src/item/itm_character.li
@@ -38,20 +38,20 @@ Section Public
   // Data
   //
 
-  + character:STRING_CONSTANT;
+  + character:CHARACTER;
 
   //
   // Creation
   //
 
-  - create p:POSITION char n:STRING_CONSTANT :SELF <-
+  - create p:POSITION char n:CHARACTER :SELF <-
   ( + result:SELF;
     result := clone;
     result.make p char n;
     result
   );
 
-  - make p:POSITION char n:STRING_CONSTANT <-
+  - make p:POSITION char n:CHARACTER <-
   (
     position:=p;
     character:=n;
@@ -71,8 +71,4 @@ Section Public
   //
 
   - append_in buffer:STRING <-
-  (
-    buffer.add_last '\'';
-    buffer.append character;
-    buffer.add_last '\'';
-  );
+    backend.append_character character in buffer;
diff --git a/src/item/itm_string.li b/src/item/itm_string.li
index c0a9d0b..d883cd6 100644
--- a/src/item/itm_string.li
+++ b/src/item/itm_string.li
@@ -87,33 +87,11 @@ Section Public
   // Display.
   //
 
-  - append_in buffer:STRING <-
-  (
-    buffer.add_last '\"';
-    buffer.append string;
-    buffer.add_last '\"';
-  );
+  - append_in buffer:STRING <- backend.append_string string in buffer;
 
 Section Private
 
-  - length:INTEGER <-
-  ( + i,result:INTEGER;
-    i := string.lower;
-    {i <= string.upper}.while_do {
-      (string.item i = '\\').if {
-        i := i + 1;
-        (string.item i.is_digit).if {
-          i := i + 1;
-          (string.item i.is_digit).if {
-            i := i + 2;
-          };
-        };
-      };
-      result := result + 1;
-      i := i + 1;
-    };
-    result
-  );
+  - length:INTEGER <- string.count;
 
 
 
diff --git a/src/parser.li b/src/parser.li
index d526f08..fb40136 100644
--- a/src/parser.li
+++ b/src/parser.li
@@ -689,8 +689,8 @@ Section Private
   //--                 | 'v' | '\\' | '?' | '\'' | '\"' | '0'
   - read_escape_character <-
   ( + nothing:BOOLEAN;
-    + val:INTEGER;
     last_character.is_separator.if {
+      // Read escape sequence '\' {space} '\'
       position := position+1;
       {
 	(last_character = 0.to_character) ||
@@ -702,52 +702,59 @@ Section Private
 	string_tmp.remove_last 1;
 	position := position+1;
       }.elseif {last_character != 0.to_character} then {
-	syntax_error (current_position,"Unknown escape sequence.");
-      };
-    }.elseif {last_character != 0.to_character} then {
-      ( (last_character = 'a')  ||
-	{last_character = 'b'}  ||
-	{last_character = 'f'}  ||
-	{last_character = 'n'}  ||
-	{last_character = 'r'}  ||
-	{last_character = 't'}  ||
-	{last_character = 'v'}  ||
-	{last_character = '\\'} ||
-	{last_character = '?'}  ||
-	{last_character = '\''} ||
-	{last_character = '\"'}
+	syntax_error (current_position,"Malformed spacing escape sequence.");
+      };
+    }.elseif { "abfnrtv?\\\'\"".has last_character } then {
+      // Read named escape sequence
+      string_tmp.remove_last 1; // remove '\\'
+      last_character
+      .when 'a'  then { string_tmp.add_last '\a'; }
+      .when 'b'  then { string_tmp.add_last '\b'; }
+      .when 'f'  then { string_tmp.add_last '\f'; }
+      .when 'n'  then { string_tmp.add_last '\n'; }
+      .when 'r'  then { string_tmp.add_last '\r'; }
+      .when 't'  then { string_tmp.add_last '\t'; }
+      .when 'v'  then { string_tmp.add_last '\v'; }
+      .when '?'  then { string_tmp.add_last '\?'; }
+      .when '\\' then { string_tmp.add_last '\\'; }
+      .when '\'' then { string_tmp.add_last '\''; }
+      .when '\"' then { string_tmp.add_last '\"'; };
+      //string_tmp.add_last last_character;
+      position := position+1;
+    }.elseif {last_character.in_range '0' to '9'} then {
+      (
+	(last_character='0') &&
+	{position<source.upper} &&
+	{! source.item(position+1).is_hexadecimal_digit}
       ).if {
-	string_tmp.add_last last_character;
+	// Read \0
+	string_tmp.remove_last 1; // remove '\\'
+	string_tmp.add_last '\0';
+	//string_tmp.add_last last_character;
 	position := position+1;
-      }.elseif {last_character.in_range '0' to '9'} then {
-	(
-          (last_character='0') &&
-	  {position<source.upper} &&
-          {! source.item(position+1).is_hexadecimal_digit}
-        ).if {
-	  string_tmp.add_last last_character;
-	  position := position+1;
+      } else {
+	// Read '\' integer '\'
+	string_tmp2.copy string_tmp;
+	nothing := read_integer; // result is Always TRUE.
+	string_tmp.copy string_tmp2;
+	((last_integer > 255) || {last_integer < 0}).if {
+	  syntax_error (current_position,
+	  "Invalid range character number [0,255].");
+	};
+	string_tmp.remove_last 1; // remove '\\'
+	string_tmp.add_last (last_integer.to_character);
+	// Save 3 octal digits
+	//string_tmp.add_last ((val / 64).decimal_digit);
+	//string_tmp.add_last (((val % 64) / 8).decimal_digit);
+	//string_tmp.add_last ((val % 8).decimal_digit);
+	(last_character='\\').if {
+	  position := position + 1;
 	} else {
-	  string_tmp2.copy string_tmp;
-	  nothing := read_integer; // result is Always TRUE.
-	  string_tmp.copy string_tmp2;
-	  (last_integer > 255).if {
-	    syntax_error (current_position,
-	    "Invalid range character number [0,255].");
-	  };
-	  val := last_integer.to_integer;
-	  string_tmp.add_last ((val / 64).decimal_digit);
-	  string_tmp.add_last (((val % 64) / 8).decimal_digit);
-	  string_tmp.add_last ((val % 8).decimal_digit);
-	  (last_character='\\').if {
-	    position := position + 1;
-	  } else {
-	    syntax_error (current_position,"Character '\' is needed.");
-	  };
+	  syntax_error (current_position,"Character '\' is needed.");
 	};
-      } else {
-	syntax_error (current_position,"Unknown escape sequence.");
       };
+    } else {
+      syntax_error (current_position,"Unknown escape sequence.");
     };
   );
 
@@ -2048,7 +2055,10 @@ Section Private
     }.elseif {read_integer} then {
       result := ITM_NUMBER.create current_position value last_integer;
     }.elseif {read_characters} then {
-      result := ITM_CHARACTER.create current_position char last_string;
+      (last_string.count = 1).if_false {
+        syntax_error (current_position,"Character constant with more than a character");
+      };
+      result := ITM_CHARACTER.create current_position char (last_string.first);
     }.elseif {read_string} then {
       result := ITM_STRING.create current_position text last_string;
     };
diff --git a/src/tools/backend.li b/src/tools/backend.li
index 34d6f39..dd86984 100644
--- a/src/tools/backend.li
+++ b/src/tools/backend.li
@@ -32,3 +32,73 @@ Section Inherit
   + parent_any: Expanded ANY;
 
 Section Public
+
+  - append_string str:ABSTRACT_STRING in buf:STRING <-
+  (
+    buf.add_last '\"';
+    append_escaped_string str in buf;
+    buf.add_last '\"';
+  );
+
+  - append_character c:CHARACTER in buf:STRING <-
+  (
+    buf.add_last '\'';
+    append_escaped_character c in buf;
+    buf.add_last '\'';
+  );
+
+  - append_escaped_character c:CHARACTER in buf:STRING <-
+    (
+      (c = '\0').if {               buf.add_last '\\'; buf.add_last '0';
+      }.elseif {c = '\a'} then {    buf.add_last '\\'; buf.add_last 'a';
+      }.elseif {c = '\b'} then {    buf.add_last '\\'; buf.add_last 'b';
+      }.elseif {c = '\f'} then {    buf.add_last '\\'; buf.add_last 'f';
+      }.elseif {c = '\n'} then {    buf.add_last '\\'; buf.add_last 'n';
+      }.elseif {c = '\r'} then {    buf.add_last '\\'; buf.add_last 'r';
+      }.elseif {c = '\t'} then {    buf.add_last '\\'; buf.add_last 't';
+      }.elseif {c = '\v'} then {    buf.add_last '\\'; buf.add_last 'v';
+      }.elseif {c = '\\'} then {    buf.add_last '\\'; buf.add_last '\\';
+      //}.elseif {c = '\?'} then {    buf.add_last '\\'; buf.add_last '?';
+      }.elseif {c = '\v'} then {    buf.add_last '\\'; buf.add_last 'v';
+      }.elseif {c = '\''} then {    buf.add_last '\\'; buf.add_last '\'';
+      }.elseif {c = '\"'} then {    buf.add_last '\\'; buf.add_last '\"';
+      }.elseif {c.in_range '\32\' to '\126\'} then {
+        buf.add_last c;
+      } else {
+        buf.add_last '\\';
+        buf.add_last (( (c.to_integer) / 64)     .decimal_digit);
+        buf.add_last ((((c.to_integer) % 64) / 8).decimal_digit);
+        buf.add_last (( (c.to_integer) % 8 )     .decimal_digit);
+      };
+    );
+
+  - append_escaped_string str:ABSTRACT_STRING in buf:STRING <-
+    append_escaped_string str in buf split_every 0 starting 0;
+
+  - append_escaped_string str:ABSTRACT_STRING in buf:STRING split_every max:INTEGER starting t:INTEGER <-
+  ( + cur, restart :INTEGER;
+    + break_line :STRING_CONSTANT;
+    cur := buf.count - t;
+
+    is_java.if {
+      break_line := "\\\n";
+      restart := 0;
+    } else {
+      break_line := "\"\n\"";
+      restart := 1;
+    };
+
+    (str.lower).to (str.upper) do { i:INTEGER;
+      + c :CHARACTER;
+      c := str.item i;
+      append_escaped_character c in buf;
+      ((max > 0) &&
+       {(c = '\n') && {i != str.upper} ||
+        {(buf.count - cur) >= max}}).if
+      {
+        buf.append break_line;
+        cur := buf.count - restart - 1;
+      };
+    };
+  );
+

-- 
Lisaac compiler



More information about the Lisaac-commits mailing list