[erlang-questions] Bug, feature or limitation of erl_parse?

Musumeci, Antonio S Antonio.Musumeci@REDACTED
Wed Aug 25 15:22:23 CEST 2010


http://www.erlang.org/doc/apps/erts/match_spec.html

It shows that [$t|'_'] is a proper match pattern... And it does work. Since otherwise "t" ++ Else is a shortcut for [$t|Else] I was trying to find out why I appear unable to parse a string of the former to use in a matchspec as the latter one does work.

-----Original Message-----
From: erlang-questions@REDACTED [mailto:erlang-questions@REDACTED] On Behalf Of Robert Raschke
Sent: Wednesday, August 25, 2010 8:52 AM
To: erlang-questions@REDACTED
Subject: Re: [erlang-questions] Bug, feature or limitation of erl_parse?

Hmm, [$t,$e,$s,$t|'_'] is not a proper list.
Do you mean [$t,$e,$s,$t,'_'] (or the equivalent [$t,$e,$s,$t|['_']])?

If yes, then "test"++['_'] will do the match:

Eshell V5.6.5  (abort with ^G)
1> L=[$t,$e,$s,$t|['_']].
[116,101,115,116,'_']
2> "test"++['_']=L.
[116,101,115,116,'_']

But I'm pretty sure I am not understanding your question exactly.

Robby

PS Interesting, why does this work:
3> "test"++'_'.
[116,101,115,116|'_']
???

On Wed, Aug 25, 2010 at 1:17 PM, Musumeci, Antonio S < Antonio.Musumeci@REDACTED> wrote:

> Understood. Is there no way to turn "test" ++ '_' into 
> [$t,$e,$s,$t|'_'] from a string for use in a matchspec?
>
> -----Original Message-----
> From: Robert Virding [mailto:rvirding@REDACTED]
> Sent: Tuesday, August 24, 2010 5:01 PM
> To: Musumeci, Antonio S (Enterprise Infrastructure)
> Cc: erlang-questions@REDACTED
> Subject: Re: [erlang-questions] Bug, feature or limitation of erl_parse?
>
> Erl_parse does recognise '"test" ++ Else', but it is not a term it is 
> an expression. It is later inside the compiler that the expression is 
> converted into it's shortcut form. The parser just parses the 
> expression. When you did it by hand, so to speak, you got a term which 
> erl_parse could parse as a term. So:
>
> 17> f(S), {ok,S,_} = erl_scan:string("\"test\" ++ Else. ").
> {ok,[{string,1,"test"},{'++',1},{var,1,'Else'},{dot,1}],1}
> 18> erl_parse:parse_exprs(S).
> {ok,[{op,1,'++',{string,1,"test"},{var,1,'Else'}}]}
>
> N.B. it it not the string "term" which causes it to be an expression 
> but the operator '++'.
>
> Robert
>
> On 24 August 2010 22:08, Musumeci, Antonio S < 
> Antonio.Musumeci@REDACTED> wrote:
> > It doesn't appear to understand the "test" ++ Else shortcut for
> [$t,$e,$s,$t|Else]. Reason I noticed was I'm reading in terms as 
> strings and passing them to mnesia:select. erl_parse works fine with 
> [$t|'_'] but not "t" ++ '_'.
> >
> >> S = "[$t,$e,$s,$t|'_']".
> > "[$t,$e,$s,$t|'_']"
> >> T = "\"test\" ++ '_'".
> > "\"test\" ++ '_'"
> >> [$t,$e,$s,$t|'_'] = "test" ++ '_'.
> > [116,101,115,116|'_']
> >> {ok,S2,_} = erl_scan:string(S).
> > {ok,[{'[',1},
> >     {char,1,116},
> >     {',',1},
> >     {char,1,101},
> >     {',',1},
> >     {char,1,115},
> >     {',',1},
> >     {char,1,116},
> >     {'|',1},
> >     {atom,1,'_'},
> >     {']',1}],
> >    1}
> >> {ok,T2,_} = erl_scan:string(T).
> > {ok,[{string,1,"test"},{'++',1},{atom,1,'_'}],1}
> >> erl_parse:parse_term(S2++[{dot,1}]).
> > {ok,[116,101,115,116|'_']}
> >> erl_parse:parse_term(T2++[{dot,1}]).
> > {error,{1,erl_parse,"bad term"}}
> >
>


More information about the erlang-questions mailing list