Recursion hang

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Recursion hang

nyaapa
Hi, i'm trying to make some sort of perl strings -- q[abcde] -- instead of 'abcde'.
In perl i can write string like q{aaa{aaa{aaa}}} and this will be eq to "aaa{aaa{aaa}}".
I'm doing it based on your code

But when i'm running this code, i get an looped recursion

        at main.Atoms.AString.main$Atoms$AString$$bracketsMatch(AString.scala:55)
        at main.Atoms.AString.Characters(AString.scala:35)
        at main.Atoms.AString.main$Atoms$AString$$bracketsMatch(AString.scala:55)
        at main.Atoms.AString.Characters(AString.scala:35)
        at main.Atoms.AString.main$Atoms$AString$$bracketsMatch(AString.scala:55)
        at main.Atoms.AString.Characters(AString.scala:35)
        at main.Atoms.AString.main$Atoms$AString$$bracketsMatch(AString.scala:55)
        at main.Atoms.AString.Characters(AString.scala:35)


  def Characters(pair : (String, String, Boolean)) : Rule1[StringBuilder] =
    push(new StringBuilder) ~ (
      zeroOrMore("\\" ~ EscapedChar(pair) | NormalChar(pair)) |
      zeroOrMore("\\" ~ EscapedChar(pair) | NormalChar(pair)) ~
      bracketsMatch(pair) ~~% withContext((code : String, ctx) => appendToSb(code.toString)(ctx)) ~
      zeroOrMore("\\" ~ EscapedChar(pair) | NormalChar(pair))
    )

  def EscapedChar(pair : (String, String, Boolean)) =
    anyOf(s"${pair._2}\\/") ~:% withContext(appendToSb(_)(_)) |
      "b" ~ appendToSb('\b') |
      "f" ~ appendToSb('\f') |
      "n" ~ appendToSb('\n') |
      "r" ~ appendToSb('\r') |
      "t" ~ appendToSb('\t') |
      Unicode ~~% withContext((code, ctx) => appendToSb(code.asInstanceOf[Char])(ctx))

  def NormalChar(pair : (String, String, Boolean)) =
    !anyOf(s"${pair._2}\\") ~ ANY ~:% (withContext(appendToSb(_)(_)))

  def Unicode =
    "u" ~ group(Number.hDigit ~ Number.hDigit ~ Number.hDigit ~ Number.hDigit) ~> (java.lang.Integer.parseInt(_, 16))

  private def bracketsMatch(pair : (String, String, Boolean)) =
    pair._1 ~ Characters(pair) ~ pair._2 ~~> (_.toString)

  private def qString = rule { bracketsMatch(("'", "'", false)) ~~> (StringNode(_, false)) }

  private def qbString = rule { "q" ~ AString.brackets.map(bracketsMatch(_)).reduce((a, b) => a | b) ~~> (StringNode(_, false)) }

  private def qqString = rule { bracketsMatch(("\"", "\"", false)) ~~> (StringNode(_, true)) }

  private def qqbString = rule { "qq" ~ AString.brackets.map(bracketsMatch(_)).reduce((a, b) => a | b)  ~~> (StringNode(_, true)) }

  private def String : Rule1[StringNode] = rule { qString | qbString | qqString | qqbString }


  private val brackets = List(
    ("(", ")", true),
    ("{", "}", true),
    ("@", "@", false),
    ("[", "]", true),
    ("/", "/", false),
    ("'", "'", false),
    ("\"", "\"", false)
  )

Environment:
scalaVersion := "2.10.2"
libraryDependencies += "org.parboiled" % "parboiled-scala_2.10" % "1.1.6"
java version "1.7.0_45"
ubuntu 13.04 x64

What am i doing wrong?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Recursion hang

nyaapa
Hm, maybe it is a problem with bad recursion while rule compilation... but if i set bracketsMatch as
bracketsMatch : Rule1[String] = rule { ... } 
 my grammar doesn't match even double quotes strings, because of they are only second in sequenced option(i don't know what to do in this situation)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Recursion hang

nyaapa
oh, i got it, because of optimization(or caching)
    !anyOf(s"${pair._2}\\") ~ ANY ~:% (withContext(appendToSb(_)(_)))
will only be like first use, in my case ==
qString | qqString
 =>
 !anyOf(s"'\\")
 and in
qqString | qString
 =>
 !anyOf(s"\"\\")
.

Does it works like designed? Is it a legal way to make non-rules matchers?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Recursion hang

nyaapa
Finally i figured out that i can generate alternatives with classes, not methods

http://pastebin.com/pz6gLzMF

maybe some improvements? )
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Recursion hang

mathias
Administrator
Sorry for not replying earlier, I was at a conference last week.

I don't quite understand the problem that you are facing, those "perl-string" look quite easy to parse.
What exactly is the problem that you are trying to solve with the mutable HashMaps?

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 23.10.2013, at 16:44, nyaapa [via parboiled users] <[hidden email]> wrote:

>
>
> Finally i figured out that i need can generate alternatives with classes, not
> methods
>
> http://pastebin.com/pz6gLzMF
>
> maybe some improvements? )
>
>
>
> _______________________________________________
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Recursion-hang-tp4024249p4024252.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, visit class="small">
signature.asc (858 bytes)
Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Recursion hang

nyaapa
they are mutable only for init, maybe it will be good to make them immutable after init or make (true, false).map(...) into immutable or i can even transform these rules to immutable list

with values of this map i'm creating or-rule
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Recursion hang

nyaapa
I was wrong, i really need a map:
i use
AString.q("\"")()
 and  
AString.q("'")()
 for match simple strings.
Or, i can have a special vals to contain single/double-quotes, but imho this will be uglier.

but mutable hashmap can be replaced with immutable, you right
Loading...