Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement raw strings #46

Merged
merged 9 commits into from
Jan 18, 2024
1 change: 1 addition & 0 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ func typeCheck(param *parameterDefinition, argument *actionArgument) {
}
case argValueType == Question:
case argValueType == Nil:
case param.validType == String && argument.valueType == RawString:
case argument.valueType != param.validType:
if argValueType == String {
argVal = "\"" + argVal.(string) + "\""
Expand Down
25 changes: 25 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ func collectValue(valueType *tokenType, value *any, until rune) {
if strings.ContainsAny(stringValue, "{}") {
checkInlineVars(&stringValue)
}
case char == '\'':
advance()
*valueType = RawString
*value = collectRawString()
advance()
case char == '[':
advance()
*valueType = Arr
Expand Down Expand Up @@ -1113,6 +1118,26 @@ func collectString() string {
return collection.String()
}

func collectRawString() string {
var collection strings.Builder
for char != -1 {
if char == '\\' && next(1) == '\'' {
collection.WriteRune('\'')
advanceTimes(2)
continue
}
if char == '\'' {
break
}

collection.WriteRune(char)
advance()
}
advance()

return collection.String()
}

func collectArray() (array interface{}) {
var rawJSON = "{\"array\":[" + collectUntilIgnoreStrings(']') + "]}"
if err := json.Unmarshal([]byte(rawJSON), &array); err != nil {
Expand Down
14 changes: 14 additions & 0 deletions plist.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ func makeVariableValue(outputName *plistData, UUID *plistData, valueType tokenTy
makeBoolValue(outputName, UUID, value)
case String:
makeStringValue(outputName, UUID, value)
case RawString:
makeRawStringValue(outputName, UUID, value)
case Expression:
makeExpressionValue(outputName, UUID, value)
case Action:
Expand Down Expand Up @@ -212,6 +214,18 @@ func makeStringValue(outputName *plistData, UUID *plistData, value *any) {
}))
}

func makeRawStringValue(outputName *plistData, UUID *plistData, value *any) {
appendPlist(makeStdAction("gettext", []plistData{
*outputName,
*UUID,
{
key: "WFTextActionText",
dataType: Text,
value: fmt.Sprintf("%s", *value),
},
}))
}

func makeBoolValue(outputName *plistData, UUID *plistData, value *any) {
var boolValue = "0"
if *value == true {
Expand Down
3 changes: 2 additions & 1 deletion tests/variables.cherri
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ const immutable = 5
@multiString = "Multiline
string
var"
@rawString = 'i\'m not allowed inline variables, new lines, etc. but i compile faster!'
@arrayVar = ["item 1 {intVar}","item 2","item 3",5,{"key1":"value","key2":5,"key3":true}]
@boolVarTrue = true
@boolVarFalse = false
@urlVar = url("https://apple.com","https://google.com")
@urlVar = url('https://apple.com','https://google.com')
@dateVar = date("October 5, 2022")
@email = emailAddress("test@test.com")
@phone = phoneNumber("(555) 555-5555")
Expand Down
1 change: 1 addition & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (

const (
String tokenType = "text"
RawString tokenType = "rawtext"
Integer tokenType = "number"
Dict tokenType = "dictionary"
Arr tokenType = "array"
Expand Down