diff --git a/bin/precompile.pike b/bin/precompile.pike index 1a9779c29168ed06a29a46f4e1e6c751bc52332e..23bc24a3b1f7f49fc664fefe5ef09b4a001d6966 100644 --- a/bin/precompile.pike +++ b/bin/precompile.pike @@ -72,46 +72,9 @@ int parse_type(array x, int pos) } } -array(PC.Token) strip(array(PC.Token) t) -{ - array ret=({}); - foreach(t, mixed x) - { - if(objectp(x)) - { - switch(x->text[0]) - { - case ' ': - case '\t': - case '\n': - case '\r': - continue; - - case '/': - if(strlen(x->text)>1) - { - switch(x->text[1]) - { - case '/': - case '*': - continue; - } - } - } - }else{ - x=strip(x); - } - ret+=({x}); - } - return ret; -} - string merge(array x) { - string ret=""; - foreach(x,x) - ret+=arrayp(x)?merge(x):objectp(x)?x->text:x; - return ret; + return PC.simple_reconstitute(x); } string cname(mixed type) @@ -241,6 +204,12 @@ string make_pop(mixed howmany) } } +/* Fixme: + * This routine inserts non-tokenized strings into the data, which + * can confuse a later stage, we might need to do something about that. + * However, I need a *simple* way of doing it first... + * -Hubbe + */ array fix_return(array body, string rettype, string ctype, mixed args) { int pos=0; @@ -292,8 +261,6 @@ array recursive(mixed func, array data, mixed ... args) mapping parse_attributes(array attr) { - attr=strip(attr); - mapping attributes=([]); foreach(attr/ ({";"}), attr) { @@ -331,7 +298,7 @@ array convert(array x) for(p=0;p<sizeof(func);p++) if(arrayp(func[p]) && func[p][0]=="{") break; - array proto=strip(func[..p-1]); + array proto=func[..p-1]; array body=func[p]; string name=proto[p]->text; mapping attributes=parse_attributes(proto[p+2..]); @@ -370,7 +337,7 @@ array convert(array x) if(arrayp(func[p]) && func[p][0]=="{") break; - array proto=strip(func[..p-1]); + array proto=func[..p-1]; array body=func[p]; array rest=func[p+1..]; @@ -519,6 +486,7 @@ int main(int argc, array(string) argv) x=Stdio.read_file(file); x=PC.split(x); x=PC.tokenize(x,file); + x=PC.hide_whitespaces(x); x=PC.group(x); array tmp=convert(x); diff --git a/lib/modules/Parser.pmod/C.pmod b/lib/modules/Parser.pmod/C.pmod index 876d208dfd02f2ad77abacf2326c27ff2712a512..9d8cc7f16f47c189e9ca14831ed23bc695894377 100644 --- a/lib/modules/Parser.pmod/C.pmod +++ b/lib/modules/Parser.pmod/C.pmod @@ -190,12 +190,14 @@ class Token int line; string text; string file; + string trailing_whitespaces=""; - void create(string t, int l, void|string f) + void create(string t, int l, void|string f, void|string space) { text=t; line=l; file=f; + if(space) trailing_whitespaces=space; } string _sprintf(int how) @@ -230,6 +232,9 @@ class Token } } +/* FIXME: + * Check for #line statements + */ array(Token) tokenize(array(string) s, void|string file) { array(Token) ret=allocate(sizeof(s)); @@ -242,7 +247,6 @@ array(Token) tokenize(array(string) s, void|string file) return ret; } - array group(array(Token) tokens, void|mapping groupings) { array(Token) ret=({}); @@ -265,6 +269,33 @@ array group(array(Token) tokens, void|mapping groupings) return ret; } +array hide_whitespaces(array tokens) +{ + array(Token) ret=({tokens[0]}); + foreach(tokens[1..], array|object(Token) t) + { + if(arrayp(t)) + { + ret+=({ hide_whitespaces(t) }); + }else{ + switch( ((string)t) [0]) + { + case ' ': + case '\t': + case '\n': + mixed tmp=ret[-1]; + while(arrayp(tmp)) tmp=tmp[-1]; + tmp->trailing_whitespaces+=(string)t; + break; + + default: + ret+=({t}); + } + } + } + return ret; +} + /* This module must work with Pike 7.0 */ #if constant(Array.flatten) #define FLATTEN Array.flatten @@ -278,9 +309,17 @@ array flatten(array a) } #endif -string simple_reconstitute(array(Token) tokens) +string simple_reconstitute(array(string|object(Token)|array) tokens) { - return FLATTEN(tokens->text) * ""; + string ret=""; + foreach(FLATTEN(tokens), mixed tok) + { + if(objectp(tok)) + tok=tok->text + tok->trailing_whitespaces; + ret+=tok; + } + + return ret; } string reconstitute_with_line_numbers(array(string|object(Token)|array) tokens) @@ -300,7 +339,7 @@ string reconstitute_with_line_numbers(array(string|object(Token)|array) tokens) if(tok->file) file=tok->file; ret+=sprintf("#line %d %O\n",line,file); } - tok=tok->text; + tok=tok->text + tok->trailing_whitespaces; } ret+=tok; line+=sizeof(tok/"\n")-1;