Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from MoeOrganization/prakashk/quotesque
regexes with brackets as delimiters and double-quoted string interpolation
- Loading branch information
Showing
18 changed files
with
374 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"StatementsNode" : [{"StringLiteralNode" : "hello world"}]} | ||
{"StatementsNode" : [{"StringSequenceNode" : {"stringParts" : [{"StringLiteralNode" : "hello world"}]}}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"StatementsNode" : [{"StringSequenceNode" : {"stringParts" : [{"StringLiteralNode" : "foo\\tbar\\n"}]}}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"StatementsNode" : [{"StringLiteralNode" : "foo\"bar\""}]} | ||
{"StatementsNode" : [{"StringSequenceNode" : {"stringParts" : [{"StringLiteralNode" : "foo\"bar\""}]}}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"StatementsNode" : [{"StringLiteralNode" : "\\x{03a3}"}]} | ||
{"StatementsNode" : [{"StringSequenceNode" : {"stringParts" : [{"StringLiteralNode" : "\\x{03a3}"}]}}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"StatementsNode" : [{"ArrayLiteralNode" : [{"IntLiteralNode" : 10}, {"StringLiteralNode" : "twenty"}, {"FloatLiteralNode" : 30.5}, {"BooleanLiteralNode" : "false"}]}]} | ||
{"StatementsNode" : [{"ArrayLiteralNode" : [{"IntLiteralNode" : 10}, {"StringSequenceNode" : {"stringParts" : [{"StringLiteralNode" : "twenty"}]}}, {"FloatLiteralNode" : 30.5}, {"BooleanLiteralNode" : "false"}]}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"StatementsNode" : [{"ArrayLiteralNode" : [{"IntLiteralNode" : 10}, {"ArrayLiteralNode" : [{"IntLiteralNode" : 20}, {"StringLiteralNode" : "thirty"}]}, {"IntLiteralNode" : 40}, {"ArrayLiteralNode" : [{"BooleanLiteralNode" : "true"}]}]}]} | ||
{"StatementsNode" : [{"ArrayLiteralNode" : [{"IntLiteralNode" : 10}, {"ArrayLiteralNode" : [{"IntLiteralNode" : 20}, {"StringSequenceNode" : {"stringParts" : [{"StringLiteralNode" : "thirty"}]}}]}, {"IntLiteralNode" : 40}, {"ArrayLiteralNode" : [{"BooleanLiteralNode" : "true"}]}]}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"StatementsNode" : [{"HashLiteralNode" : [{"PairLiteralNode" : {"key" : {"StringLiteralNode" : "one"}, "value" : {"IntLiteralNode" : 10}}}, {"PairLiteralNode" : {"key" : {"StringLiteralNode" : "two"}, "value" : {"IntLiteralNode" : 20}}}, {"PairLiteralNode" : {"key" : {"StringLiteralNode" : "three"}, "value" : {"IntLiteralNode" : 30}}}]}]} | ||
{"StatementsNode" : [{"HashLiteralNode" : [{"PairLiteralNode" : {"key" : {"StringSequenceNode" : {"stringParts" : [{"StringLiteralNode" : "one"}]}}, "value" : {"IntLiteralNode" : 10}}}, {"PairLiteralNode" : {"key" : {"StringSequenceNode" : {"stringParts" : [{"StringLiteralNode" : "two"}]}}, "value" : {"IntLiteralNode" : 20}}}, {"PairLiteralNode" : {"key" : {"StringSequenceNode" : {"stringParts" : [{"StringLiteralNode" : "three"}]}}, "value" : {"IntLiteralNode" : 30}}}]}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package org.moe.parser | ||
|
||
import scala.util.parsing.combinator._ | ||
import scala.util.matching.Regex | ||
|
||
trait MoeQuoteParser extends JavaTokenParsers { | ||
val quotePairMap = Map( | ||
'(' -> ')', | ||
'[' -> ']', | ||
'{' -> '}', | ||
'<' -> '>' | ||
) | ||
|
||
private def uptoEndDelim(in: Input, beginDelim: Char, endDelim: Char) = { | ||
val source = in.source | ||
val offset = in.offset | ||
var j = offset + 1 | ||
// beginDelim found; copy input upto endDelim | ||
val buf = new StringBuilder | ||
val pairedDelim = beginDelim != endDelim | ||
|
||
var nestingLevel = 0; | ||
var done = false; | ||
while (j < source.length && !done) { | ||
source.charAt(j) match { | ||
case '\\' if source.charAt(j+1) == beginDelim || source.charAt(j+1) == endDelim=> { | ||
j += 1 | ||
buf += source.charAt(j) | ||
} | ||
case `beginDelim` => { | ||
if (pairedDelim) { | ||
nestingLevel += 1 | ||
buf += source.charAt(j) | ||
} | ||
else { | ||
done = true | ||
} | ||
} | ||
case `endDelim` => { | ||
if (nestingLevel == 0) { | ||
done = true | ||
} | ||
else { | ||
nestingLevel -= 1 | ||
buf += source.charAt(j) | ||
} | ||
} | ||
case _ => { | ||
buf += source.charAt(j) | ||
} | ||
} | ||
j += 1 | ||
} | ||
|
||
if (done) { | ||
(Some(buf.toString), in.drop(j - offset - 1)) | ||
} | ||
else { | ||
(None, in.drop(j - offset - 1)) | ||
} | ||
} | ||
|
||
def quoted(q: Char): Parser[String] = new Parser[String] { | ||
def apply(in: Input) = { | ||
val source = in.source | ||
val offset = in.offset | ||
val start = handleWhiteSpace(source, offset) | ||
// println(s"source = |$source|, offset = |$offset|, start = |$start|") | ||
|
||
var j = start | ||
val beginDelim = q | ||
val endDelim = quotePairMap.getOrElse(beginDelim, beginDelim) | ||
|
||
if (j < source.length && beginDelim != source.charAt(j)) { | ||
Failure("`" + q + "' expected but not found", in.drop(start - offset)) | ||
} | ||
else { | ||
uptoEndDelim(in.drop(start - offset), beginDelim, endDelim) match { | ||
case (Some(matched), rest_in) => Success(matched, rest_in.drop(1)) // drop endDelim from input | ||
case (None, rest_in) => Failure("`" + endDelim + "' expected but not found.", rest_in) | ||
} | ||
} | ||
} | ||
} | ||
|
||
def quoted(q: Regex): Parser[String] = new Parser[String] { | ||
def apply(in: Input) = { | ||
val source = in.source | ||
val offset = in.offset | ||
val start = handleWhiteSpace(source, offset) | ||
|
||
(q findPrefixMatchOf(source.subSequence(start, source.length))) match { | ||
case Some(matched) => { | ||
val beginDelim = source.subSequence(start, start + matched.end).charAt(0) | ||
val endDelim = quotePairMap.getOrElse(beginDelim, beginDelim) | ||
uptoEndDelim(in.drop(start - offset), beginDelim, endDelim) match { | ||
case (Some(matched), rest_in) => Success(matched, rest_in.drop(1)) // drop endDelim from input | ||
case (None, rest_in) => Failure("`" + endDelim + "' expected but not found.", rest_in) | ||
} | ||
} | ||
case None => | ||
Failure("`string matching " + q + "' expected but not found", in.drop(start - offset)) | ||
} | ||
} | ||
} | ||
|
||
def quotedPair(q: Char): Parser[(String, String)] = new Parser[(String, String)] { | ||
def apply(in: Input) = { | ||
val source = in.source | ||
val offset = in.offset | ||
val start = handleWhiteSpace(source, offset) | ||
// println(s"source = |$source|, offset = |$offset|, start = |$start|") | ||
|
||
var j = start | ||
val beginDelim = q | ||
val endDelim = quotePairMap.getOrElse(beginDelim, beginDelim) | ||
|
||
if (j < source.length && beginDelim != source.charAt(j)) { | ||
Failure("`" + q + "' expected but not found", in.drop(start - offset)) | ||
} | ||
else { | ||
uptoEndDelim(in.drop(start - offset), beginDelim, endDelim) match { | ||
case (Some(matched_1), rest_in) => { | ||
uptoEndDelim(rest_in, beginDelim, endDelim) match { | ||
case (Some(matched_2), rest_in_2) => Success((matched_1, matched_2), rest_in_2.drop(1)) // drop endDelim from input | ||
case (None, rest_in_2) => Failure("`" + endDelim + "' expected but not found.", rest_in_2) | ||
} | ||
} | ||
case (None, rest_in) => Failure("`" + endDelim + "' expected but not found.", rest_in) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.