-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add test code for grammar specification #2
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
16d689f
Add test code
sigmaith bccdd44
Handle syntax errors and print error messages
sigmaith 8ad6ec6
Add yorkie specific data types to grammar
sigmaith f652a32
Add annotations
sigmaith 0b4934e
Modify test code's mistakes
sigmaith a684894
Fix grammar of yorkie element level types
sigmaith ea9f9c3
Enhance type safety in validate function's error handling
sigmaith e694b8d
Merge branch 'main' into schema_validation
sigmaith 2e66708
Add newline at end of file
sigmaith a2b9afc
Add ANTLR grammar build files
sigmaith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,46 +1,110 @@ | ||
grammar Schema; | ||
|
||
// Entry point of the grammar | ||
start: typeDefinitions EOF; | ||
typeDefinitions | ||
: typeDefinition (WHITESPACE* typeDefinitions)? | ||
; | ||
typeDefinition | ||
: 'type' IDENTIFIER '{' fieldList '}' | ||
; | ||
fieldList | ||
: field (WHITESPACE* fieldList)? | ||
; | ||
field | ||
: IDENTIFIER WHITESPACE* ':' WHITESPACE* fieldType | ||
; | ||
fieldType | ||
: typeExpression arraySuffix* | ||
; | ||
typeExpression | ||
: unionType | ||
| simpleType | ||
; | ||
simpleType | ||
: primitiveType | ||
| IDENTIFIER | ||
; | ||
arraySuffix | ||
: '[]' | ||
; | ||
unionType | ||
: '(' WHITESPACE* unionTypeInner WHITESPACE* ')' | ||
| unionTypeInner | ||
; | ||
unionTypeInner | ||
: simpleType (WHITESPACE* '|' WHITESPACE* simpleType)* | ||
; | ||
primitiveType | ||
: ('string' | 'number' | 'boolean') | ||
; | ||
IDENTIFIER | ||
: [a-zA-Z_] [a-zA-Z0-9_]* | ||
; | ||
WHITESPACE | ||
: [ \t\n\r]+ -> skip | ||
; | ||
// Lexer rules | ||
IDENTIFIER: [a-zA-Z_] [a-zA-Z0-9_]*; | ||
NEWLINE: '\r'? '\n' -> skip; | ||
COMMENT: ('//' | '#') ~[\r\n]* -> skip; | ||
DIGIT: [0-9]; | ||
WHITESPACE: [ \t\n\r]+ -> skip; | ||
|
||
// Keywords | ||
YORKIE_OBJECT: 'yorkie.Object'; | ||
YORKIE_ARRAY: 'yorkie.Array'; | ||
YORKIE_COUNTER: 'yorkie.Counter'; | ||
YORKIE_TEXT: 'yorkie.Text'; | ||
YORKIE_TREE: 'yorkie.Tree'; | ||
|
||
// Operations and Symbols | ||
MINUS: '-'; | ||
SEMICOLON: ';'; | ||
LPAREN: '('; | ||
RPAREN: ')'; | ||
LCURLY: '{'; | ||
RCURLY: '}'; | ||
GT: '>'; | ||
LT: '<'; | ||
PIPE: '|'; | ||
QUESTION: '?'; | ||
EQ: '='; | ||
COMMA: ','; | ||
LSQUARE: '['; | ||
RSQUARE: ']'; | ||
|
||
// Utils | ||
DOUBLE_QUOTED_STRING: '"' (ESC | ~["\n])* '"'; | ||
SINGLE_QUOTED_STRING: '\'' (ESC | ~['\n])* '\''; | ||
fragment ESC: '\\' [btnr\\'"]; | ||
|
||
// Top-level rule | ||
document: definitionList EOF; | ||
|
||
definitionList: definition (WHITESPACE* definition)*; | ||
|
||
definition: objectTypeDefinition; | ||
|
||
typeName: IDENTIFIER; | ||
|
||
objectTypeDefinition | ||
: 'type' WHITESPACE* typeName WHITESPACE* LCURLY WHITESPACE* | ||
fieldDefList? WHITESPACE* RCURLY SEMICOLON? | ||
; | ||
|
||
fieldDefList | ||
: fieldDef ((COMMA | SEMICOLON | NEWLINE) fieldDef)* | ||
; | ||
|
||
identifier: IDENTIFIER; | ||
|
||
fieldDef: identifier QUESTION? ':' type; | ||
|
||
type: nonUnionType (PIPE type)*; | ||
|
||
nonUnionType: nonUnionTypeL2 (LSQUARE RSQUARE)*; | ||
|
||
nonUnionTypeL2: | ||
LPAREN type RPAREN | ||
| objectLiteralType | ||
| primitiveType | ||
| literalType | ||
| yorkieType | ||
| typeReference; | ||
|
||
typeReference: typeName; | ||
objectLiteralType: LCURLY fieldDefList? RCURLY; | ||
|
||
primitiveType: | ||
'string' | ||
| 'number' | ||
| 'boolean' | ||
| 'null' | ||
| 'bigint' | ||
| 'Uint8Array' | ||
| 'Date' | ||
; | ||
|
||
literalType: | ||
booleanLiteralType | ||
| numberLiteralType | ||
| stringLiteralType; | ||
|
||
booleanLiteralType: 'true' | 'false'; | ||
|
||
numberLiteralType: MINUS? DIGIT+ ('.' DIGIT+)?; | ||
sigmaith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
stringLiteralType: DOUBLE_QUOTED_STRING | SINGLE_QUOTED_STRING; | ||
sigmaith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
yorkieType: | ||
yorkieObjectType | ||
| yorkieArrayType | ||
| yorkieCounterType | ||
| yorkieTextType | ||
| yorkieTreeType; | ||
|
||
yorkieObjectType: | ||
YORKIE_OBJECT LT (typeReference | objectLiteralType) GT; | ||
yorkieArrayType: | ||
YORKIE_ARRAY LT (typeReference | objectLiteralType) GT; | ||
yorkieCounterType: YORKIE_COUNTER; | ||
yorkieTextType: | ||
YORKIE_TEXT LT (typeReference | objectLiteralType) GT; | ||
yorkieTreeType: YORKIE_TREE LT GT; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider standardizing field separators
The 'fieldDefList' rule currently allows for multiple separators (comma, semicolon, newline) between field definitions. While this provides flexibility, it may lead to inconsistencies in schema definitions.
Consider standardizing on a single separator for clarity and consistency. For example:
This change would enforce a consistent style across schema definitions.