Skip to content

Commit

Permalink
ignores consecutive escaped characters
Browse files Browse the repository at this point in the history
  • Loading branch information
jennyafish committed Jul 3, 2019
1 parent d727c6c commit 503925f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
34 changes: 28 additions & 6 deletions examples/tsls/formatstring.wyv
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ def parseExpr(input : String, ctx : system.Context) : ParsedExpr
makeParsedExpr(option.None[AST](), input)


//val prefixRegex : regex.Regex = regex("(\\\\|\\%|[^%])+")
//alternative approach: if last char of prefixString is \ and next matches %, replace it with % and append to parseTSL
//would need to replace \ in string even if using regex
//val prefixRegex : regex.Regex = regex("(\\\\\\\\|\\\\%|[^%])+")
val prefixRegex : regex.Regex = regex("[^\\\\%]+")
//val prefixRegex : regex.Regex = regex("[^%]+")

Expand Down Expand Up @@ -174,10 +172,34 @@ def parse(input : String, ctx : system.Context) : option.Option[AST]
else
option.None[AST]() //recursive call gives None
else
option.None[AST]() //cannot parse expression
// can only reach here if first character starts with \
if (input.charAt(0) == #"\\")
if (input.length() >= 2)
if (input.charAt(1) == #"%")
val recursiveCall : option.Option[AST] = parse(input.substring(2,input.length()), ctx)
if (recursiveCall.isDefined)
option.Some[AST](ast.call(ast.string("%"), "+", {recursiveCall.get()}))
else
option.None[AST]()
else
if (input.charAt(1) == #"\\")
val recursiveCall : option.Option[AST] = parse(input.substring(2,input.length()), ctx)
if (recursiveCall.isDefined)
option.Some[AST](ast.call(ast.string("\\"), "+", {recursiveCall.get()}))
else
option.None[AST]()
else
val recursiveCall : option.Option[AST] = parse(input.substring(1,input.length()), ctx)
if (recursiveCall.isDefined)
option.Some[AST](ast.call(ast.string("\\"), "+", {recursiveCall.get()}))
else
option.None[AST]()
else
option.Some[AST](ast.string(input))
else
option.None[AST]() //cannot parse expression


//include \% + other characters, except pure %
//TODO: ignore escape character (\%)
type FormatString = String
metadata new
def parseTSL(input : String, ctx : system.Context) : option.Option[AST]
Expand Down
6 changes: 2 additions & 4 deletions examples/tsls/formatstringClient.wyv
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ def printf(str:formatstring.FormatString):Unit

//just a plain string
printf(~)
Hello \\ \%World!

/*
Hello \\\%World!\n

printf(~)
Hello %{"string!"}%{" \"}{mm}"} and %{ "another string!" }

val x = 3
printf(~)
%{x} %{4} %.5{1.6} %{1.7}
*/

0 comments on commit 503925f

Please sign in to comment.