From 8edb6922068fcacfac784bbc46a473a227089ffa Mon Sep 17 00:00:00 2001 From: miheevks Date: Sat, 1 Oct 2022 03:10:02 +0300 Subject: [PATCH] support pragma ton-solidity --- src/main/grammars/_SolidityLexer.flex | 6 ++- src/main/grammars/grammar.txt | 2 +- src/main/grammars/solidity.bnf | 3 +- .../SolidityFormattingModelBuilder.kt | 3 +- src/test/resources/fixtures/parser/error.txt | 4 +- .../resources/fixtures/parser/fallback.txt | 4 +- .../resources/fixtures/parser/natSpec.txt | 4 +- src/test/resources/fixtures/parser/pragma.sol | 3 ++ src/test/resources/fixtures/parser/pragma.txt | 38 +++++++++++++++---- .../resources/fixtures/parser/tryCatch.txt | 4 +- .../fixtures/parser/userDefinedValueTypes.txt | 4 +- src/test/resources/fixtures/parser/using.txt | 4 +- 12 files changed, 55 insertions(+), 24 deletions(-) diff --git a/src/main/grammars/_SolidityLexer.flex b/src/main/grammars/_SolidityLexer.flex index 88189830..6dc03b0a 100644 --- a/src/main/grammars/_SolidityLexer.flex +++ b/src/main/grammars/_SolidityLexer.flex @@ -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=[^ ][^;]* @@ -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 /* @@ -286,9 +288,9 @@ PRAGMAALL=[^ ][^;]* { {WHITE_SPACE} { return WHITE_SPACE; } - {IDENTIFIER} { + {PRAGMAIDENTIFIER} { yybegin(PRAGMA_REST); - return IDENTIFIER; + return PRAGMAIDENTIFIER; } <> { yybegin(YYINITIAL); } diff --git a/src/main/grammars/grammar.txt b/src/main/grammars/grammar.txt index 538bfa6a..05f27390 100644 --- a/src/main/grammars/grammar.txt +++ b/src/main/grammars/grammar.txt @@ -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 ';' diff --git a/src/main/grammars/solidity.bnf b/src/main/grammars/solidity.bnf index 538bb18c..e69056ea 100644 --- a/src/main/grammars/solidity.bnf +++ b/src/main/grammars/solidity.bnf @@ -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=':' @@ -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 } diff --git a/src/main/kotlin/me/serce/solidity/ide/formatting/SolidityFormattingModelBuilder.kt b/src/main/kotlin/me/serce/solidity/ide/formatting/SolidityFormattingModelBuilder.kt index 0845edb0..6f4f2043 100644 --- a/src/main/kotlin/me/serce/solidity/ide/formatting/SolidityFormattingModelBuilder.kt +++ b/src/main/kotlin/me/serce/solidity/ide/formatting/SolidityFormattingModelBuilder.kt @@ -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) diff --git a/src/test/resources/fixtures/parser/error.txt b/src/test/resources/fixtures/parser/error.txt index 375ea76d..6b6c471f 100644 --- a/src/test/resources/fixtures/parser/error.txt +++ b/src/test/resources/fixtures/parser/error.txt @@ -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(;)(';') @@ -113,4 +113,4 @@ Solidity File PsiWhiteSpace('\n ') PsiElement(})('}') PsiWhiteSpace('\n') - PsiElement(})('}') \ No newline at end of file + PsiElement(})('}') diff --git a/src/test/resources/fixtures/parser/fallback.txt b/src/test/resources/fixtures/parser/fallback.txt index 0dd425be..ddb303dd 100644 --- a/src/test/resources/fixtures/parser/fallback.txt +++ b/src/test/resources/fixtures/parser/fallback.txt @@ -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(;)(';') @@ -47,4 +47,4 @@ Solidity File PsiWhiteSpace('\n\n ') PsiElement(})('}') PsiWhiteSpace('\n') - PsiElement(})('}') \ No newline at end of file + PsiElement(})('}') diff --git a/src/test/resources/fixtures/parser/natSpec.txt b/src/test/resources/fixtures/parser/natSpec.txt index 7d194964..c0c228d9 100644 --- a/src/test/resources/fixtures/parser/natSpec.txt +++ b/src/test/resources/fixtures/parser/natSpec.txt @@ -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(;)(';') @@ -337,4 +337,4 @@ Solidity File PsiWhiteSpace('\n ') PsiElement(})('}') PsiWhiteSpace('\n') - PsiElement(})('}') \ No newline at end of file + PsiElement(})('}') diff --git a/src/test/resources/fixtures/parser/pragma.sol b/src/test/resources/fixtures/parser/pragma.sol index 4267daa5..faa603c1 100644 --- a/src/test/resources/fixtures/parser/pragma.sol +++ b/src/test/resources/fixtures/parser/pragma.sol @@ -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; diff --git a/src/test/resources/fixtures/parser/pragma.txt b/src/test/resources/fixtures/parser/pragma.txt index 3fdcd092..cebad744 100644 --- a/src/test/resources/fixtures/parser/pragma.txt +++ b/src/test/resources/fixtures/parser/pragma.txt @@ -2,7 +2,7 @@ Solidity File SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE) PsiElement(pragma)('pragma') PsiWhiteSpace(' ') - PsiElement(Identifier)('solidity') + PsiElement(pragmaIdentifier)('solidity') PsiWhiteSpace(' ') PsiElement(pragmaAll)('^0.4.2') PsiElement(;)(';') @@ -10,7 +10,7 @@ Solidity File SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE) PsiElement(pragma)('pragma') PsiWhiteSpace(' ') - PsiElement(Identifier)('solidity') + PsiElement(pragmaIdentifier)('solidity') PsiWhiteSpace(' ') PsiElement(pragmaAll)('0.4.2') PsiElement(;)(';') @@ -18,7 +18,7 @@ Solidity File SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE) PsiElement(pragma)('pragma') PsiWhiteSpace(' ') - PsiElement(Identifier)('solidity') + PsiElement(pragmaIdentifier)('solidity') PsiWhiteSpace(' ') PsiElement(pragmaAll)('>0.4.23 <0.5.0') PsiElement(;)(';') @@ -26,7 +26,31 @@ Solidity File 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(;)(';') @@ -34,7 +58,7 @@ Solidity File SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE) PsiElement(pragma)('pragma') PsiWhiteSpace(' ') - PsiElement(Identifier)('experimental') + PsiElement(pragmaIdentifier)('experimental') PsiWhiteSpace(' ') PsiElement(pragmaAll)(''v0.5.0'') PsiElement(;)(';') @@ -42,7 +66,7 @@ Solidity File SolPragmaDirectiveImpl(PRAGMA_DIRECTIVE) PsiElement(pragma)('pragma') PsiWhiteSpace(' ') - PsiElement(Identifier)('experimental') + PsiElement(pragmaIdentifier)('experimental') PsiWhiteSpace(' ') PsiElement(pragmaAll)('ABIEncoderV2') - PsiElement(;)(';') \ No newline at end of file + PsiElement(;)(';') diff --git a/src/test/resources/fixtures/parser/tryCatch.txt b/src/test/resources/fixtures/parser/tryCatch.txt index ab3b188b..1e53af30 100644 --- a/src/test/resources/fixtures/parser/tryCatch.txt +++ b/src/test/resources/fixtures/parser/tryCatch.txt @@ -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(;)(';') @@ -285,4 +285,4 @@ Solidity File PsiWhiteSpace('\n ') PsiElement(})('}') PsiWhiteSpace('\n') - PsiElement(})('}') \ No newline at end of file + PsiElement(})('}') diff --git a/src/test/resources/fixtures/parser/userDefinedValueTypes.txt b/src/test/resources/fixtures/parser/userDefinedValueTypes.txt index 755cb828..05ba98ec 100644 --- a/src/test/resources/fixtures/parser/userDefinedValueTypes.txt +++ b/src/test/resources/fixtures/parser/userDefinedValueTypes.txt @@ -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(;)(';') @@ -454,4 +454,4 @@ Solidity File PsiWhiteSpace('\n ') PsiElement(})('}') PsiWhiteSpace('\n') - PsiElement(})('}') \ No newline at end of file + PsiElement(})('}') diff --git a/src/test/resources/fixtures/parser/using.txt b/src/test/resources/fixtures/parser/using.txt index 7112bf52..b1d4df45 100644 --- a/src/test/resources/fixtures/parser/using.txt +++ b/src/test/resources/fixtures/parser/using.txt @@ -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(;)(';') @@ -191,4 +191,4 @@ Solidity File PsiWhiteSpace('\n ') PsiElement(})('}') PsiWhiteSpace('\n') - PsiElement(})('}') \ No newline at end of file + PsiElement(})('}')