Skip to content

Commit

Permalink
Add Support for Time Literals In SOQL Queries
Browse files Browse the repository at this point in the history
  • Loading branch information
cwarden committed Dec 16, 2024
1 parent dc0954f commit e397a4c
Show file tree
Hide file tree
Showing 14 changed files with 2,756 additions and 2,641 deletions.
19 changes: 17 additions & 2 deletions formatter/soql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/antlr4-go/antlr/v4"
"github.com/octoberswimmer/apexfmt/parser"
Expand Down Expand Up @@ -56,10 +57,24 @@ func (f *SOQLFormatter) Format() error {
p.AddErrorListener(&errorListener{})

v := NewFormatVisitor(stream)
out, ok := v.visitRule(p.Query()).(string)
out, ok := v.visitRule(p.SoqlLiteral()).(string)
if !ok {
return fmt.Errorf("Unexpected result parsing apex")
}
f.formatted = append([]byte(out), '\n')
f.formatted = append([]byte(unindent(out)), '\n')
return nil
}

func unindent(input string) string {
input = strings.TrimPrefix(input, "[")
input = strings.TrimSuffix(input, "]")
input = strings.TrimSpace(input)

lines := strings.Split(input, "\n")

for i, line := range lines {
lines[i] = strings.TrimPrefix(line, "\t")
}

return strings.Join(lines, "\n")
}
4 changes: 4 additions & 0 deletions formatter/visitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,10 @@ func (v *FormatVisitor) VisitDateTimeLiteralValue(ctx *parser.DateTimeLiteralVal
return ctx.GetText()
}

func (v *FormatVisitor) VisitTimeLiteralValue(ctx *parser.TimeLiteralValueContext) interface{} {
return ctx.GetText()
}

func (v *FormatVisitor) VisitDateFormulaValue(ctx *parser.DateFormulaValueContext) interface{} {
return v.visitRule(ctx.DateFormula())
}
Expand Down
3 changes: 2 additions & 1 deletion grammar/ApexLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ N_FISCAL_YEARS_AGO : N '_' F I S C A L '_' Y E A R S '_' A G O;

// SOQL Date literal
DateLiteral: Digit Digit Digit Digit '-' Digit Digit '-' Digit Digit;
DateTimeLiteral: DateLiteral T Digit Digit ':' Digit Digit ':' Digit Digit (Z | (('+' | '-') Digit+ ( ':' Digit+)? ));
TimeLiteral: Digit Digit ':' Digit Digit ':' Digit Digit ('.' Digit+ )? (Z | (('+' | '-') Digit+ ( ':' Digit+)? ));
DateTimeLiteral: DateLiteral T TimeLiteral;

// SOQL Currency literal
// (NOTE: this is also a valid Identifier)
Expand Down
1 change: 1 addition & 0 deletions grammar/ApexParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ value
| signedNumber # signedNumberValue
| StringLiteral # stringLiteralValue
| DateLiteral # dateLiteralValue
| TimeLiteral # timeLiteralValue
| DateTimeLiteral # dateTimeLiteralValue
| dateFormula # dateFormulaValue
| currencyValue # currencyValueValue
Expand Down
5 changes: 4 additions & 1 deletion parser/ApexLexer.interp

Large diffs are not rendered by default.

251 changes: 126 additions & 125 deletions parser/ApexLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -169,128 +169,129 @@ N_FISCAL_QUARTERS_AGO=168
N_YEARS_AGO=169
N_FISCAL_YEARS_AGO=170
DateLiteral=171
DateTimeLiteral=172
IntegralCurrencyLiteral=173
FIND=174
EMAIL=175
NAME=176
PHONE=177
SIDEBAR=178
FIELDS=179
METADATA=180
PRICEBOOKID=181
NETWORK=182
SNIPPET=183
TARGET_LENGTH=184
DIVISION=185
RETURNING=186
LISTVIEW=187
FindLiteral=188
FindLiteralAlt=189
IntegerLiteral=190
LongLiteral=191
NumberLiteral=192
BooleanLiteral=193
StringLiteral=194
NullLiteral=195
LPAREN=196
RPAREN=197
LBRACE=198
RBRACE=199
LBRACK=200
RBRACK=201
SEMI=202
COMMA=203
DOT=204
ASSIGN=205
GT=206
LT=207
BANG=208
TILDE=209
QUESTIONDOT=210
QUESTION=211
COLON=212
EQUAL=213
TRIPLEEQUAL=214
NOTEQUAL=215
LESSANDGREATER=216
TRIPLENOTEQUAL=217
AND=218
OR=219
COAL=220
INC=221
DEC=222
ADD=223
SUB=224
MUL=225
DIV=226
BITAND=227
BITOR=228
CARET=229
MOD=230
MAPTO=231
ADD_ASSIGN=232
SUB_ASSIGN=233
MUL_ASSIGN=234
DIV_ASSIGN=235
AND_ASSIGN=236
OR_ASSIGN=237
XOR_ASSIGN=238
MOD_ASSIGN=239
LSHIFT_ASSIGN=240
RSHIFT_ASSIGN=241
URSHIFT_ASSIGN=242
ATSIGN=243
Identifier=244
WS=245
DOC_COMMENT=246
COMMENT=247
LINE_COMMENT=248
'('=196
')'=197
'{'=198
'}'=199
'['=200
']'=201
';'=202
','=203
'.'=204
'='=205
'>'=206
'<'=207
'!'=208
'~'=209
'?.'=210
'?'=211
':'=212
'=='=213
'==='=214
'!='=215
'<>'=216
'!=='=217
'&&'=218
'||'=219
'??'=220
'++'=221
'--'=222
'+'=223
'-'=224
'*'=225
'/'=226
'&'=227
'|'=228
'^'=229
'%'=230
'=>'=231
'+='=232
'-='=233
'*='=234
'/='=235
'&='=236
'|='=237
'^='=238
'%='=239
'<<='=240
'>>='=241
'>>>='=242
'@'=243
TimeLiteral=172
DateTimeLiteral=173
IntegralCurrencyLiteral=174
FIND=175
EMAIL=176
NAME=177
PHONE=178
SIDEBAR=179
FIELDS=180
METADATA=181
PRICEBOOKID=182
NETWORK=183
SNIPPET=184
TARGET_LENGTH=185
DIVISION=186
RETURNING=187
LISTVIEW=188
FindLiteral=189
FindLiteralAlt=190
IntegerLiteral=191
LongLiteral=192
NumberLiteral=193
BooleanLiteral=194
StringLiteral=195
NullLiteral=196
LPAREN=197
RPAREN=198
LBRACE=199
RBRACE=200
LBRACK=201
RBRACK=202
SEMI=203
COMMA=204
DOT=205
ASSIGN=206
GT=207
LT=208
BANG=209
TILDE=210
QUESTIONDOT=211
QUESTION=212
COLON=213
EQUAL=214
TRIPLEEQUAL=215
NOTEQUAL=216
LESSANDGREATER=217
TRIPLENOTEQUAL=218
AND=219
OR=220
COAL=221
INC=222
DEC=223
ADD=224
SUB=225
MUL=226
DIV=227
BITAND=228
BITOR=229
CARET=230
MOD=231
MAPTO=232
ADD_ASSIGN=233
SUB_ASSIGN=234
MUL_ASSIGN=235
DIV_ASSIGN=236
AND_ASSIGN=237
OR_ASSIGN=238
XOR_ASSIGN=239
MOD_ASSIGN=240
LSHIFT_ASSIGN=241
RSHIFT_ASSIGN=242
URSHIFT_ASSIGN=243
ATSIGN=244
Identifier=245
WS=246
DOC_COMMENT=247
COMMENT=248
LINE_COMMENT=249
'('=197
')'=198
'{'=199
'}'=200
'['=201
']'=202
';'=203
','=204
'.'=205
'='=206
'>'=207
'<'=208
'!'=209
'~'=210
'?.'=211
'?'=212
':'=213
'=='=214
'==='=215
'!='=216
'<>'=217
'!=='=218
'&&'=219
'||'=220
'??'=221
'++'=222
'--'=223
'+'=224
'-'=225
'*'=226
'/'=227
'&'=228
'|'=229
'^'=230
'%'=231
'=>'=232
'+='=233
'-='=234
'*='=235
'/='=236
'&='=237
'|='=238
'^='=239
'%='=240
'<<='=241
'>>='=242
'>>>='=243
'@'=244
4 changes: 3 additions & 1 deletion parser/ApexParser.interp

Large diffs are not rendered by default.

Loading

0 comments on commit e397a4c

Please sign in to comment.