Hey all,
I'm working on an implementation of
JOY programming language for fun (I highly recommend the language and would love to discuss it with other monks!)
My current task: I'm splitting up an input file into a list of tokens. I've slurped the whole thing into a string and am trying to split it.
The string should be split at each whitespace (\s+)
unless the space is between unescaped parentheses. There will only one level of parentheses, so no need to worry about deep nesting.
For example,
this is (some more)
JOY code
should split into
"this", "is", "(some more)", "JOY", "code"
I thought I had a good solution: split on whitespace when we can look behind for a ) and ahead for a (.
# match when between unescaped reverse parens
+
# ..or start/end of string
+
@script = split
/
(?<= #lookbehind (only remove whitespaces)
+
(^| #for start of string or
+
(?<!\\) \) ) #unescaped rparen followed by
+
.*? #stingy other chars
+
)
\s+ #some spaces, to be removed
+
(?= #lookahead
+
.*? #stingy other chars
+
($| #end of string or
+
(?<!\\) \( ) #unescaped lparen
+
)
/xs, $script;
but when I run this, I get "Variable length lookbehind is not implemented in regex".
So, what would you do? I'm not sure it's possible with regular expressions anymore (a challenge!) since lookbehind was my way of not removing anything but the whitespace... should I keep regex hacking, or move on?
Thanks,
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.