diff options
author | Adrian Thurston <thurston@complang.org> | 2013-03-21 22:10:56 -0400 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2013-03-21 22:10:56 -0400 |
commit | e8d9cf2d25e1f2570e6edbd017d63ea5159eb99f (patch) | |
tree | 4e0981e23cbdd3542b0d5ab3dc1c36f9a6b3f9e3 | |
parent | a4962e36dab578f8c9bce690fd6a3cf474b32ea5 (diff) | |
download | colm-e8d9cf2d25e1f2570e6edbd017d63ea5159eb99f.tar.gz |
added strings
-rw-r--r-- | colm/colm.lm | 24 | ||||
-rw-r--r-- | colm/load.cc | 154 | ||||
-rw-r--r-- | colm/load.h | 17 | ||||
-rw-r--r-- | test/treecmp1.exp | 1 | ||||
-rw-r--r-- | test/treecmp1.in | 1 | ||||
-rw-r--r-- | test/treecmp1.lm | 5 |
6 files changed, 165 insertions, 37 deletions
diff --git a/colm/colm.lm b/colm/colm.lm index 3b528a54..dce0f83b 100644 --- a/colm/colm.lm +++ b/colm/colm.lm @@ -282,6 +282,7 @@ def code_factor OptFieldInit: opt_field_init Accumulate: accumulate] | [Cons: CONS OptCapture: opt_capture TypeRef: type_ref OptFieldInit: opt_field_init Constructor: constructor] +| [String: cstring] def type_ref [RegionQual: region_qual DirectId: id OptRepeat: opt_repeat] @@ -313,6 +314,29 @@ def field_init [CodeExpr: code_expr] # +# String List +# + +def string_el +# ['"' lit_string_el* '"'] + [CodeExpr: code_expr] + +def lit_string_el + [DLit: dlit] +# ['[' string_el* ']'] + +def string_top_el + [DQ LitStringElList: lit_string_el* TDQ] +| [SQOPEN StringElList: string_el* SQOPEN] + +def string_list + [StringTopEl: string_top_el StringList: string_list] +| [StringTopEl: string_top_el] + +def cstring + [StringList: string_list] + +# # Constructor List # diff --git a/colm/load.cc b/colm/load.cc index e51254bb..74dad87a 100644 --- a/colm/load.cc +++ b/colm/load.cc @@ -35,6 +35,36 @@ extern RuntimeData main_runtimeData; +String unescape( const String &s ) +{ + String out( String::Fresh(), s.length() ); + char *d = out.data; + + for ( int i = 0; i < s.length(); ) { + if ( s[i] == '\\' ) { + switch ( s[i+1] ) { + case '0': *d++ = '\0'; break; + case 'a': *d++ = '\a'; break; + case 'b': *d++ = '\b'; break; + case 't': *d++ = '\t'; break; + case 'n': *d++ = '\n'; break; + case 'v': *d++ = '\v'; break; + case 'f': *d++ = '\f'; break; + case 'r': *d++ = '\r'; break; + default: *d++ = s[i+1]; break; + } + i += 2; + } + else { + *d++ = s[i]; + i += 1; + } + } + *d = 0; + return out; +} + + NamespaceQual *LoadSource::walkRegionQual( region_qual regionQual ) { NamespaceQual *qual; @@ -199,45 +229,16 @@ void LoadSource::walkProdList( LelDefList *lelDefList, prod_list &ProdList ) prodAppend( lelDefList, prod ); } -String transReChars( const String &s ) -{ - String out( String::Fresh(), s.length() ); - char *d = out.data; - - for ( int i = 0; i < s.length(); ) { - if ( s[i] == '\\' ) { - switch ( s[i+1] ) { - case '0': *d++ = '\0'; break; - case 'a': *d++ = '\a'; break; - case 'b': *d++ = '\b'; break; - case 't': *d++ = '\t'; break; - case 'n': *d++ = '\n'; break; - case 'v': *d++ = '\v'; break; - case 'f': *d++ = '\f'; break; - case 'r': *d++ = '\r'; break; - default: *d++ = s[i+1]; break; - } - i += 2; - } - else { - *d++ = s[i]; - i += 1; - } - } - *d = 0; - return out; -} - ReOrItem *LoadSource::walkRegOrChar( reg_or_char regOrChar ) { ReOrItem *orItem = 0; if ( regOrChar.Char() != 0 ) { - String c = transReChars( regOrChar.Char().text().c_str() ); + String c = unescape( regOrChar.Char().text().c_str() ); orItem = ReOrItem::cons( internal, c ); } else { - String low = transReChars( regOrChar.Low().text().c_str() ); - String high = transReChars( regOrChar.High().text().c_str() ); + String low = unescape( regOrChar.Low().text().c_str() ); + String high = unescape( regOrChar.High().text().c_str() ); orItem = ReOrItem::cons( internal, low[0], high[0] ); } return orItem; @@ -506,11 +507,92 @@ ObjectField *walkOptCapture( opt_capture optCapture ) return objField; } +/* + * String + */ + +ConsItemList *LoadSource::walkLitStringEl( lit_string_el litStringEl ) +{ + ConsItemList *list = 0; + if ( litStringEl.DLit() != 0 ) { + String dlit = unescape( litStringEl.DLit().text().c_str() ); + ConsItem *stringItem = ConsItem::cons( internal, ConsItem::InputText, dlit ); + list = ConsItemList::cons( stringItem ); + } + return list; +} + +ConsItemList *LoadSource::walkLitStringElList( _repeat_lit_string_el litStringElList ) +{ + ConsItemList *list = new ConsItemList; + while ( !litStringElList.end() ) { + ConsItemList *extension = walkLitStringEl( litStringElList.value() ); + list = consListConcat( list, extension ); + litStringElList = litStringElList.next(); + } + return list; +} + +ConsItemList *LoadSource::walkStringEl( string_el stringEl ) +{ + ConsItemList *list = 0; + if ( stringEl.CodeExpr() != 0 ) { + LangExpr *consExpr = walkCodeExpr( stringEl.CodeExpr() ); + ConsItem *consItem = ConsItem::cons( internal, ConsItem::ExprType, consExpr ); + list = ConsItemList::cons( consItem ); + } + return list; +} + +ConsItemList *LoadSource::walkStringElList( _repeat_string_el stringElList ) +{ + ConsItemList *list = new ConsItemList; + while ( !stringElList.end() ) { + ConsItemList *extension = walkStringEl( stringElList.value() ); + list = consListConcat( list, extension ); + stringElList = stringElList.next(); + } + return list; +} + +ConsItemList *LoadSource::walkStringTopEl( string_top_el stringTopEl ) +{ + ConsItemList *list = 0; + if ( stringTopEl.LitStringElList() != 0 ) + list = walkLitStringElList( stringTopEl.LitStringElList() ); + else if ( stringTopEl.StringElList() != 0 ) { + list = walkStringElList( stringTopEl.StringElList() ); + } + return list; +} + +ConsItemList *LoadSource::walkStringList( string_list stringList ) +{ + ConsItemList *list = walkStringTopEl( stringList.StringTopEl() ); + + if ( stringList.StringList() != 0 ) { + ConsItemList *extension = walkStringList( stringList.StringList() ); + consListConcat( list, extension ); + } + + return list; +} + +ConsItemList *LoadSource::walkString( cstring String ) +{ + ConsItemList *list = walkStringList( String.StringList() ); + return list; +} + +/* + * Constructor + */ + ConsItemList *LoadSource::walkLitConsEl( lit_cons_el litConsEl ) { ConsItemList *list = 0; if ( litConsEl.DLit() != 0 ) { - String dlit = litConsEl.DLit().text().c_str(); + String dlit = unescape( litConsEl.DLit().text().c_str() ); ConsItem *consItem = ConsItem::cons( internal, ConsItem::InputText, dlit ); list = ConsItemList::cons( consItem ); } @@ -587,7 +669,7 @@ ConsItemList *LoadSource::walkLitAccumEl( lit_accum_el litAccumEl ) { ConsItemList *list = 0; if ( litAccumEl.DLit() != 0 ) { - String dlit = litAccumEl.DLit().text().c_str(); + String dlit = unescape( litAccumEl.DLit().text().c_str() ); ConsItem *consItem = ConsItem::cons( internal, ConsItem::InputText, dlit ); list = ConsItemList::cons( consItem ); } @@ -741,6 +823,10 @@ LangExpr *LoadSource::walkCodeFactor( code_factor codeFactor ) else if ( codeFactor.ParenCodeExpr() != 0 ) { expr = walkCodeExpr( codeFactor.ParenCodeExpr() ); } + else if ( codeFactor.String() != 0 ) { + ConsItemList *list = walkString( codeFactor.String() ); + expr = LangExpr::cons( LangTerm::cons( internal, list ) ); + } return expr; } diff --git a/colm/load.h b/colm/load.h index 9c588f2b..eeaceca7 100644 --- a/colm/load.h +++ b/colm/load.h @@ -86,6 +86,13 @@ struct cons_el; struct _repeat_cons_el; struct code_relational; struct code_additive; +struct lit_string_el; +struct _repeat_lit_string_el; +struct string_top_el; +struct string_list; +struct cstring; +struct string_el; +struct _repeat_string_el; struct LoadSource : @@ -168,8 +175,16 @@ struct LoadSource ConsItemList *walkConsList( cons_list consList ); ConsItemList *walkConstructor( constructor Constructor ); ConsItemList *walkConsEl( cons_el consEl ); - ConsItemList *walkConsElList( _repeat_cons_el accumElList ); + ConsItemList *walkConsElList( _repeat_cons_el consElList ); LangExpr *walkCodeRelational( code_relational codeRelational ); LangExpr *walkCodeAdditive( code_additive codeAdditive ); + + ConsItemList *walkLitStringEl( lit_string_el litStringEl ); + ConsItemList *walkLitStringElList( _repeat_lit_string_el litStringElList ); + ConsItemList *walkStringTopEl( string_top_el stringTopEl ); + ConsItemList *walkStringList( string_list stringList ); + ConsItemList *walkString( cstring String ); + ConsItemList *walkStringEl( string_el stringEl ); + ConsItemList *walkStringElList( _repeat_string_el stringElList ); }; diff --git a/test/treecmp1.exp b/test/treecmp1.exp deleted file mode 100644 index 61780798..00000000 --- a/test/treecmp1.exp +++ /dev/null @@ -1 +0,0 @@ -b diff --git a/test/treecmp1.in b/test/treecmp1.in deleted file mode 100644 index ff852910..00000000 --- a/test/treecmp1.in +++ /dev/null @@ -1 +0,0 @@ -a b c d
\ No newline at end of file diff --git a/test/treecmp1.lm b/test/treecmp1.lm index c8f351e3..178f10d7 100644 --- a/test/treecmp1.lm +++ b/test/treecmp1.lm @@ -1,3 +1,4 @@ +##### LM ##### rl ident_pattern /[a-zA-Z_][a-zA-Z_0-9]*/ rl number_pattern /[0-9]+/ @@ -19,3 +20,7 @@ for Id: id in Input { if ( Id == B ) print( B '\n' ) } +##### IN ##### +a b c d +##### EXP ##### +b |