[erlang-questions] clarify: escaping strings in erl_scan:string / erl_parse:parse_term

Roberto Saccon rsaccon@REDACTED
Fri Dec 28 16:20:10 CET 2007


oh yeah, that's it, Vlad and Corrado, thanks a lot !

On Dec 28, 2007 11:52 AM, Corrado Santoro <csanto@REDACTED> wrote:
> You have to quote the backslashes:
>
> 7> {ok,Tokens,_}=erl_scan:string("{a_string, \"foo(\\\"hi\\\")\"}.").
> {ok,[{'{',1},
>       {atom,1,a_string},
>       {',',1},
>       {string,1,"foo(\"hi\")"},
>       {'}',1},
>       {dot,1}],
>      1}
> 8> erl_parse:parse_term(Tokens).
> {ok,{a_string,"foo(\"hi\")"}}
>
> :-)
>
> Cheers,
> --Corrado
>
>
> Roberto Saccon wrote:
> > How do I escape strings  in Erlang before tokenizing / parsing with
> > erl_scan:string/1 and erl_parse:parse_term/1 ?
> >
> > E.g. in the interactive Erlang shell I simply do:
> >
> > 1> {a_string, "foo(\"hi\")"}.
> > {a_string,"foo(\"hi\")"}
> >
> >
> > But if I try to do the same with erl_scan:string and
> > erl_parse:parse_term, it is tokenizing not as expected and parsing
> > gives an error:
> >
> >
> > 2> {ok,Tokens,_}=erl_scan:string("{a_string, \"foo(\"hi\")\"}.").
> > {ok,[{'{',1},
> >      {atom,1,a_string},
> >      {',',1},
> >      {string,1,"foo("},
> >      {atom,1,hi},
> >      {string,1,")"},
> >      {'}',1},
> >      {dot,1}],
> >     1}
> > 3> erl_parse:parse_term(Tokens).
> > {error,{1,erl_parse,["syntax error before: ","hi"]}}
> > 4>
> >
> >
> > So what is the proper way to escape strings in this context ?
> >
>
>
> --
> ==================================================================
> Eng. Corrado Santoro, Ph.D.
> University of Catania - ITALY - Engineering Faculty
>
> Tel: +39 095 7382380        VoIP: sip:7035@REDACTED
>
> Personal Home Page: http://www.diit.unict.it/users/csanto
>      NUXI Home Page: http://nuxi.diit.unict.it
> ==================================================================
>
>



-- 
Roberto Saccon
http://rsaccon.com



More information about the erlang-questions mailing list