Skip to content

Commit

Permalink
support pragma ton-solidity
Browse files Browse the repository at this point in the history
  • Loading branch information
miheevks committed Oct 1, 2022
1 parent bf18d2f commit 8edb692
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 24 deletions.
6 changes: 4 additions & 2 deletions src/main/grammars/_SolidityLexer.flex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ UFIXEDNUMTYPE=ufixed(0x8|0x16|0x24|0x32|0x40|0x48|0x56|0x64|0x72|0x80|0x88|0x96|
BOOLEANLITERAL=true|false
SPACE=[ \t\n\x0B\f\r]+
IDENTIFIER=[a-zA-Z_$][a-zA-Z_$0-9]*
PRAGMAIDENTIFIER=[a-zA-Z_$][a-zA-Z_$0-9-]*
// Pragma parses anything up to the trailing ';' to be fully forward-compatible.
PRAGMAALL=[^ ][^;]*

Expand Down Expand Up @@ -228,6 +229,7 @@ PRAGMAALL=[^ ][^;]*
{BOOLEANLITERAL} { return BOOLEANLITERAL; }
{SPACE} { return SPACE; }
{IDENTIFIER} { return IDENTIFIER; }
{PRAGMAIDENTIFIER} { return PRAGMAIDENTIFIER; }
}

// nested block comments are not supported, so don't track the occurrences of /*
Expand Down Expand Up @@ -286,9 +288,9 @@ PRAGMAALL=[^ ][^;]*
<PRAGMA_OPEN> {
{WHITE_SPACE} { return WHITE_SPACE; }

{IDENTIFIER} {
{PRAGMAIDENTIFIER} {
yybegin(PRAGMA_REST);
return IDENTIFIER;
return PRAGMAIDENTIFIER;
}

<<EOF>> { yybegin(YYINITIAL); }
Expand Down
2 changes: 1 addition & 1 deletion src/main/grammars/grammar.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SourceUnit = (PragmaDirective | ImportDirective | ContractDefinition)*

// Pragma actually parses anything up to the trailing ';' to be fully forward-compatible.
PragmaDirective = 'pragma' Identifier ([^;]+) ';'
PragmaDirective = 'pragma' PragmaIdentifier ([^;]+) ';'

ImportDirective = 'import' StringLiteral ('as' Identifier)? ';'
| 'import' ('*' | Identifier) ('as' Identifier)? 'from' StringLiteral ';'
Expand Down
3 changes: 2 additions & 1 deletion src/main/grammars/solidity.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
space='regexp:\s+'

Identifier='regexp:[a-zA-Z_$][a-zA-Z_$0-9]*'
pragmaIdentifier='regexp:[a-zA-Z_$][a-zA-Z_$0-9-]*'

DOT='.'
COLON=':'
Expand Down Expand Up @@ -110,7 +111,7 @@ private UntilSemicolonRecover ::= !(';')
private UntilBraceRecover ::= !('}')

// Pragma actually parses anything up to the trailing ';' to be fully forward-compatible.
private PragmaPart ::= pragma Identifier pragmaAll {
private PragmaPart ::= pragma pragmaIdentifier pragmaAll {
pin = 2
recoverWhile = UntilSemicolonRecover
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class SolidityFormattingModelBuilder : FormattingModelBuilder {
.after(CONTROL_STRUCTURES).spaces(1)
.beforeInside(STATEMENT, TokenSet.create(IF_STATEMENT, WHILE_STATEMENT, FOR_STATEMENT, DO_WHILE_STATEMENT)).spaces(1)
.after(CONTRACT).spaces(1)
.aroundInside(IDENTIFIER, TokenSet.create(CONTRACT_DEFINITION, PRAGMA_DIRECTIVE, STRUCT_DEFINITION, PARAMETER_DEF)).spaces(1)
.aroundInside(IDENTIFIER, TokenSet.create(CONTRACT_DEFINITION, STRUCT_DEFINITION, PARAMETER_DEF)).spaces(1)
.aroundInside(PRAGMAIDENTIFIER, PRAGMA_DIRECTIVE).spaces(1)
.afterInside(TYPE_NAME, TokenSet.create(VARIABLE_DECLARATION, PARAMETER_LIST, INDEXED_PARAMETER_LIST)).spaces(1)
.beforeInside(IDENTIFIER, TokenSet.create(VARIABLE_DECLARATION, PARAMETER_LIST, INDEXED_PARAMETER_LIST)).spaces(1)
.after(COMMA).spaces(1)
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/fixtures/parser/error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Solidity File
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(Identifier)('solidity')
PsiElement(pragmaIdentifier)('solidity')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('^0.8.4')
PsiElement(;)(';')
Expand Down Expand Up @@ -113,4 +113,4 @@ Solidity File
PsiWhiteSpace('\n ')
PsiElement(})('}')
PsiWhiteSpace('\n')
PsiElement(})('}')
PsiElement(})('}')
4 changes: 2 additions & 2 deletions src/test/resources/fixtures/parser/fallback.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Solidity File
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(Identifier)('solidity')
PsiElement(pragmaIdentifier)('solidity')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('>=0.7.0 <0.9.0')
PsiElement(;)(';')
Expand Down Expand Up @@ -47,4 +47,4 @@ Solidity File
PsiWhiteSpace('\n\n ')
PsiElement(})('}')
PsiWhiteSpace('\n')
PsiElement(})('}')
PsiElement(})('}')
4 changes: 2 additions & 2 deletions src/test/resources/fixtures/parser/natSpec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Solidity File
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(Identifier)('solidity')
PsiElement(pragmaIdentifier)('solidity')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('>=0.8.2 < 0.9.0')
PsiElement(;)(';')
Expand Down Expand Up @@ -337,4 +337,4 @@ Solidity File
PsiWhiteSpace('\n ')
PsiElement(})('}')
PsiWhiteSpace('\n')
PsiElement(})('}')
PsiElement(})('}')
3 changes: 3 additions & 0 deletions src/test/resources/fixtures/parser/pragma.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
pragma solidity ^0.4.2;
pragma solidity 0.4.2;
pragma solidity >0.4.23 <0.5.0;
pragma ton-solidity ^0.7.3;
pragma ton-solidity 0.7.3;
pragma ton-solidity >0.7.15 <0.8.0;
pragma experimental "v0.5.0";
pragma experimental 'v0.5.0';
pragma experimental ABIEncoderV2;
Expand Down
38 changes: 31 additions & 7 deletions src/test/resources/fixtures/parser/pragma.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,71 @@ Solidity File
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(Identifier)('solidity')
PsiElement(pragmaIdentifier)('solidity')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('^0.4.2')
PsiElement(;)(';')
PsiWhiteSpace('\n')
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(Identifier)('solidity')
PsiElement(pragmaIdentifier)('solidity')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('0.4.2')
PsiElement(;)(';')
PsiWhiteSpace('\n')
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(Identifier)('solidity')
PsiElement(pragmaIdentifier)('solidity')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('>0.4.23 <0.5.0')
PsiElement(;)(';')
PsiWhiteSpace('\n')
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(Identifier)('experimental')
PsiElement(pragmaIdentifier)('ton-solidity')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('^0.7.3')
PsiElement(;)(';')
PsiWhiteSpace('\n')
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(pragmaIdentifier)('ton-solidity')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('0.7.3')
PsiElement(;)(';')
PsiWhiteSpace('\n')
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(pragmaIdentifier)('ton-solidity')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('>0.7.15 <0.8.0')
PsiElement(;)(';')
PsiWhiteSpace('\n')
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(pragmaIdentifier)('experimental')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('"v0.5.0"')
PsiElement(;)(';')
PsiWhiteSpace('\n')
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(Identifier)('experimental')
PsiElement(pragmaIdentifier)('experimental')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)(''v0.5.0'')
PsiElement(;)(';')
PsiWhiteSpace('\n')
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(Identifier)('experimental')
PsiElement(pragmaIdentifier)('experimental')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('ABIEncoderV2')
PsiElement(;)(';')
PsiElement(;)(';')
4 changes: 2 additions & 2 deletions src/test/resources/fixtures/parser/tryCatch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Solidity File
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(Identifier)('solidity')
PsiElement(pragmaIdentifier)('solidity')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('^0.6.0')
PsiElement(;)(';')
Expand Down Expand Up @@ -285,4 +285,4 @@ Solidity File
PsiWhiteSpace('\n ')
PsiElement(})('}')
PsiWhiteSpace('\n')
PsiElement(})('}')
PsiElement(})('}')
4 changes: 2 additions & 2 deletions src/test/resources/fixtures/parser/userDefinedValueTypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Solidity File
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(Identifier)('solidity')
PsiElement(pragmaIdentifier)('solidity')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('^0.8.8')
PsiElement(;)(';')
Expand Down Expand Up @@ -454,4 +454,4 @@ Solidity File
PsiWhiteSpace('\n ')
PsiElement(})('}')
PsiWhiteSpace('\n')
PsiElement(})('}')
PsiElement(})('}')
4 changes: 2 additions & 2 deletions src/test/resources/fixtures/parser/using.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Solidity File
SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE)
PsiElement(pragma)('pragma')
PsiWhiteSpace(' ')
PsiElement(Identifier)('solidity')
PsiElement(pragmaIdentifier)('solidity')
PsiWhiteSpace(' ')
PsiElement(pragmaAll)('>=0.7.0 <0.9.0')
PsiElement(;)(';')
Expand Down Expand Up @@ -191,4 +191,4 @@ Solidity File
PsiWhiteSpace('\n ')
PsiElement(})('}')
PsiWhiteSpace('\n')
PsiElement(})('}')
PsiElement(})('}')

0 comments on commit 8edb692

Please sign in to comment.