From abaecf807f96f7f34442717ccfcb0fb500895988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=9F=D0=BE=D0=B3=D0=BE=D1=80=D0=B5=D0=BB=D0=BE=D0=B2?= Date: Thu, 23 May 2019 10:37:16 +0300 Subject: [PATCH] Formatter fixes, code examples. --- CodeCompletion/CodeFormatter.cs | 1 + .../patterns-collection-matching1.pas | 14 +++--- .../patterns-collection-matching2.pas | 8 ++-- .../patterns-collection-matching3.pas | 4 +- .../Patterns/patterns-simple-const.pas | 2 +- .../Patterns/patterns-simple-tuple.pas | 4 +- .../Patterns/patterns-type-matching1.pas | 6 +-- .../PascalABCParserNewSaushkin/ABCPascal.cs | 2 +- .../PascalABCParserNewSaushkin/ABCPascal.y | 2 +- .../ABCPascalYacc.cs | 4 +- TestSuite/formatter_tests/input/pattern2.pas | 46 +++++++++++++++++++ TestSuite/formatter_tests/input/pattern3.pas | 7 +++ TestSuite/formatter_tests/should/pattern2.pas | 46 +++++++++++++++++++ TestSuite/formatter_tests/should/pattern3.pas | 7 +++ .../patterns-collection-matching1.pas | 4 +- .../patterns-collection-matching2.pas | 6 +-- .../patterns-collection-matching3.pas | 2 +- TestSuite/patterns/patterns-simple-tuple.pas | 2 +- .../patterns/patterns-type-matching1.pas | 4 +- 19 files changed, 139 insertions(+), 32 deletions(-) create mode 100644 TestSuite/formatter_tests/input/pattern2.pas create mode 100644 TestSuite/formatter_tests/input/pattern3.pas create mode 100644 TestSuite/formatter_tests/should/pattern2.pas create mode 100644 TestSuite/formatter_tests/should/pattern3.pas diff --git a/CodeCompletion/CodeFormatter.cs b/CodeCompletion/CodeFormatter.cs index 2c6565f7f..958ce6d9e 100644 --- a/CodeCompletion/CodeFormatter.cs +++ b/CodeCompletion/CodeFormatter.cs @@ -3205,6 +3205,7 @@ namespace CodeFormatters public override void visit(wild_card_deconstructor_parameter _wild_card_deconstructor_parameter) { + sb.Append("_"); add_space_after = true; } diff --git a/CodeExamples/Patterns/patterns-collection-matching1.pas b/CodeExamples/Patterns/patterns-collection-matching1.pas index 822e3bf6b..f4f644c05 100644 --- a/CodeExamples/Patterns/patterns-collection-matching1.pas +++ b/CodeExamples/Patterns/patterns-collection-matching1.pas @@ -1,13 +1,13 @@ type CardInfo = auto class - public + public cardNumber: string; cv: integer; end; Person = class - public + public name: string; age: integer; card: CardInfo; @@ -28,16 +28,16 @@ end; begin - var a := Arr(1,9,8,7,2,3,4,5); + var a := Arr(1, 9, 8, 7, 2, 3, 4, 5); // Расширенный is - if a is [1,..,x,_,5] then // a is [1,..,1,1,1,1,1,1,1,1,1,1] - ран тайм эррор - - Println(x); //в расширенном is короткое вычисление логических выражений не работает (#1508) + if a is [1, .., var x, _, 5] then + Println(x); // match .. with match a with - [1,9,8,'str',2]: print(1); //Нет проверки соответствия типов в для коллекций - добавить. - [..,4,x]: print(x); + [1, 9, 8, _, 2]: print(1); + [.., var y, var x]: print(x + y); [_, .., _]: print(3); end; end. \ No newline at end of file diff --git a/CodeExamples/Patterns/patterns-collection-matching2.pas b/CodeExamples/Patterns/patterns-collection-matching2.pas index 0eaadb4bb..8d4ce8724 100644 --- a/CodeExamples/Patterns/patterns-collection-matching2.pas +++ b/CodeExamples/Patterns/patterns-collection-matching2.pas @@ -1,13 +1,13 @@ type CardInfo = auto class - public + public cardNumber: string; cv: integer; end; Person = class - public + public name: string; age: integer; card: CardInfo; @@ -39,8 +39,8 @@ begin // match .. with match personArr with - [_,_,Person('Вася', age, _)]: print(age); - [p, .., Person('Маша', _, _)]: print((p as Person).name); + [_, _, Person('Вася', age, _)]: print(age); + [var p, .., Person('Маша', _, _)]: print((p as Person).name); [..]: print(1); end; end. \ No newline at end of file diff --git a/CodeExamples/Patterns/patterns-collection-matching3.pas b/CodeExamples/Patterns/patterns-collection-matching3.pas index 0a0b732c9..444fcd657 100644 --- a/CodeExamples/Patterns/patterns-collection-matching3.pas +++ b/CodeExamples/Patterns/patterns-collection-matching3.pas @@ -1,6 +1,6 @@ type CardInfo = auto class - public + public cardNumber: string; cv: integer; end; @@ -37,7 +37,7 @@ begin // match .. with match p with Person('Петя', _, _): print('Петя'); - Person('Вася', _, [_, _, x]): print(x); + Person('Вася', _, [_, _, var x]): print(x); Person(name, _, [CardInfo(_, 321), .., CardInfo(_, 324)]): print(name); end; end. \ No newline at end of file diff --git a/CodeExamples/Patterns/patterns-simple-const.pas b/CodeExamples/Patterns/patterns-simple-const.pas index bbe322374..fe66668f7 100644 --- a/CodeExamples/Patterns/patterns-simple-const.pas +++ b/CodeExamples/Patterns/patterns-simple-const.pas @@ -1,7 +1,7 @@ begin var a := 3.14; match a with - 1.0, 2.0, 3.0: Println(1); // Надо исправить: double не матчит с integer - несоответствие типов. + 1.0, 2.0, 3.0: Println(1); 3.13, 3.14, 3.15, 3.16: print(2); 123.0: print(3); end; diff --git a/CodeExamples/Patterns/patterns-simple-tuple.pas b/CodeExamples/Patterns/patterns-simple-tuple.pas index 5c622c928..7acec4bfb 100644 --- a/CodeExamples/Patterns/patterns-simple-tuple.pas +++ b/CodeExamples/Patterns/patterns-simple-tuple.pas @@ -1,7 +1,7 @@ begin match (1, 2, 'string') with - (1, _, 1): Println(1); // Нет проверки типов каждого элемента tuple - надо ли? + (1, _, var x): Println(x); (1, _, 'strin'): print(2); - (_,_,'string'): print(3); + (_, _, 'string'): print(3); end; end. \ No newline at end of file diff --git a/CodeExamples/Patterns/patterns-type-matching1.pas b/CodeExamples/Patterns/patterns-type-matching1.pas index a9edcec11..9dc7cc23e 100644 --- a/CodeExamples/Patterns/patterns-type-matching1.pas +++ b/CodeExamples/Patterns/patterns-type-matching1.pas @@ -1,6 +1,6 @@ type CardInfo = auto class - public + public cardNumber: string; cv: integer; end; @@ -29,11 +29,11 @@ begin var a := new Person('Вася', 11, new CardInfo('12345678', 324)); // Расширенный is - if a is Person('Вася', var age, CardInfo(_, var cv)) then Println(age, cv); + if a is Person(_, var age, CardInfo(_, var cv)) then Println(age, cv); // match .. with match a with - Person(1, 12, CardInfo('12345678', var cv)): Println(cv); // Нет проверки соответсвия типов при деконструировании - добавить. + Person('Петя', 12, CardInfo('12345678', var cv)): Println(cv); Person('Вася', _, CardInfo(cardNum, 324)): Println(cardNum); Person(_, _, CardInfo(_, x)): Println(x); end; diff --git a/Parsers/PascalABCParserNewSaushkin/ABCPascal.cs b/Parsers/PascalABCParserNewSaushkin/ABCPascal.cs index 2f13b42c5..14b36228e 100644 --- a/Parsers/PascalABCParserNewSaushkin/ABCPascal.cs +++ b/Parsers/PascalABCParserNewSaushkin/ABCPascal.cs @@ -2,7 +2,7 @@ // This CSharp output file generated by Gardens Point LEX // Version: 1.1.3.301 // Machine: DESKTOP-IF20NRO -// DateTime: 5/21/2019 1:27:51 PM +// DateTime: 5/23/2019 10:14:28 AM // UserName: FatCow // GPLEX input file // GPLEX frame file diff --git a/Parsers/PascalABCParserNewSaushkin/ABCPascal.y b/Parsers/PascalABCParserNewSaushkin/ABCPascal.y index 103dc046c..a2a9112de 100644 --- a/Parsers/PascalABCParserNewSaushkin/ABCPascal.y +++ b/Parsers/PascalABCParserNewSaushkin/ABCPascal.y @@ -3520,7 +3520,7 @@ pattern_out_param pattern_out_param_optional_var : tkUnderscore { - $$ = new wild_card_deconstructor_parameter(); + $$ = new wild_card_deconstructor_parameter(@$); } | literal_or_number { diff --git a/Parsers/PascalABCParserNewSaushkin/ABCPascalYacc.cs b/Parsers/PascalABCParserNewSaushkin/ABCPascalYacc.cs index c101d5c32..3119d31a1 100644 --- a/Parsers/PascalABCParserNewSaushkin/ABCPascalYacc.cs +++ b/Parsers/PascalABCParserNewSaushkin/ABCPascalYacc.cs @@ -2,7 +2,7 @@ // GPPG version 1.3.6 // Machine: DESKTOP-IF20NRO -// DateTime: 5/21/2019 1:27:52 PM +// DateTime: 5/23/2019 10:14:29 AM // UserName: FatCow // Input file @@ -5821,7 +5821,7 @@ public partial class GPPGParser: ShiftReduceParser tkUnderscore { - CurrentSemanticValue.stn = new wild_card_deconstructor_parameter(); + CurrentSemanticValue.stn = new wild_card_deconstructor_parameter(CurrentLocationSpan); } break; case 653: // pattern_out_param_optional_var -> literal_or_number diff --git a/TestSuite/formatter_tests/input/pattern2.pas b/TestSuite/formatter_tests/input/pattern2.pas new file mode 100644 index 000000000..2791c637f --- /dev/null +++ b/TestSuite/formatter_tests/input/pattern2.pas @@ -0,0 +1,46 @@ +type + CardInfo = auto class + public + cardNumber: string; + cv: integer; + end; + + Person = class + + public + name: string; + age: integer; + card: CardInfo; + + constructor(name: string; age: integer; card: CardInfo); + begin + self.name := name; + self.age := age; + self.card := card; + end; + + procedure Deconstruct(var name: string; var age: integer; var card: CardInfo); + begin + name := self.name; + age := self.age; + card := self.card; + end; + end; + +begin + var p1 := new Person('Вася', 11, new CardInfo('12345671', 321)); + var p2 := new Person('Петя', 12, new CardInfo('12345672', 322)); + var p3 := new Person('Маша', 13, new CardInfo('12345673', 323)); + var personArr := Arr(p1, p2, p3); + + // Расширенный is + if personArr is [ Person(name1 , 11, CardInfo( _, cv)), _, Person( name2, 13, _)] then + assert(name1.Equals('Вася') and (cv = 321) and name2.Equals('Маша')); + + // match .. with + match personArr with + [ _, _, Person('Вася', var age, _)]: assert(false); + [ var p, .. , Person('Маша', _, _)]: assert((p as Person).name.Equals('Вася')); + [ .. ]: assert(false); + end; +end. \ No newline at end of file diff --git a/TestSuite/formatter_tests/input/pattern3.pas b/TestSuite/formatter_tests/input/pattern3.pas new file mode 100644 index 000000000..a6b1866b9 --- /dev/null +++ b/TestSuite/formatter_tests/input/pattern3.pas @@ -0,0 +1,7 @@ +begin + match ( 1, 2, 'string' ) with + ( 1, _, 'str' ): assert(false); + ( 1, _, 'strin'): assert(false); + ( _, _, 'string'): ; + end; +end. \ No newline at end of file diff --git a/TestSuite/formatter_tests/should/pattern2.pas b/TestSuite/formatter_tests/should/pattern2.pas new file mode 100644 index 000000000..f105d5900 --- /dev/null +++ b/TestSuite/formatter_tests/should/pattern2.pas @@ -0,0 +1,46 @@ +type + CardInfo = auto class + public + cardNumber: string; + cv: integer; + end; + + Person = class + + public + name: string; + age: integer; + card: CardInfo; + + constructor(name: string; age: integer; card: CardInfo); + begin + self.name := name; + self.age := age; + self.card := card; + end; + + procedure Deconstruct(var name: string; var age: integer; var card: CardInfo); + begin + name := self.name; + age := self.age; + card := self.card; + end; + end; + +begin + var p1 := new Person('Вася', 11, new CardInfo('12345671', 321)); + var p2 := new Person('Петя', 12, new CardInfo('12345672', 322)); + var p3 := new Person('Маша', 13, new CardInfo('12345673', 323)); + var personArr := Arr(p1, p2, p3); + + // Расширенный is + if personArr is [Person(name1, 11, CardInfo(_, cv)), _, Person(name2, 13, _)] then + assert(name1.Equals('Вася') and (cv = 321) and name2.Equals('Маша')); + + // match .. with + match personArr with + [_, _, Person('Вася', var age, _)]: assert(false); + [var p, .., Person('Маша', _, _)]: assert((p as Person).name.Equals('Вася')); + [..]: assert(false); + end; +end. \ No newline at end of file diff --git a/TestSuite/formatter_tests/should/pattern3.pas b/TestSuite/formatter_tests/should/pattern3.pas new file mode 100644 index 000000000..6a59f2d67 --- /dev/null +++ b/TestSuite/formatter_tests/should/pattern3.pas @@ -0,0 +1,7 @@ +begin + match (1, 2, 'string') with + (1, _, 'str'): assert(false); + (1, _, 'strin'): assert(false); + (_, _, 'string'): ; + end; +end. \ No newline at end of file diff --git a/TestSuite/patterns/patterns-collection-matching1.pas b/TestSuite/patterns/patterns-collection-matching1.pas index 0bf1b2eb2..34e5b59f5 100644 --- a/TestSuite/patterns/patterns-collection-matching1.pas +++ b/TestSuite/patterns/patterns-collection-matching1.pas @@ -1,6 +1,6 @@ begin - var a := Arr(1,9,8,7,2,3,4,5); - var c : procedure := () -> begin + var a := Arr(1, 9, 8, 7, 2, 3, 4, 5); + var c: procedure := () -> begin match a with [.., 4, var x]: assert(x = 5); end; diff --git a/TestSuite/patterns/patterns-collection-matching2.pas b/TestSuite/patterns/patterns-collection-matching2.pas index 76bbdd8b8..17c20f571 100644 --- a/TestSuite/patterns/patterns-collection-matching2.pas +++ b/TestSuite/patterns/patterns-collection-matching2.pas @@ -1,13 +1,13 @@ type CardInfo = auto class - public + public cardNumber: string; cv: integer; end; Person = class - public + public name: string; age: integer; card: CardInfo; @@ -39,7 +39,7 @@ begin // match .. with match personArr with - [_,_,Person('Вася', var age, _)]: assert(false); + [_, _, Person('Вася', var age, _)]: assert(false); [var p, .., Person('Маша', _, _)]: assert((p as Person).name.Equals('Вася')); [..]: assert(false); end; diff --git a/TestSuite/patterns/patterns-collection-matching3.pas b/TestSuite/patterns/patterns-collection-matching3.pas index 93f3b5101..e542f0f32 100644 --- a/TestSuite/patterns/patterns-collection-matching3.pas +++ b/TestSuite/patterns/patterns-collection-matching3.pas @@ -1,6 +1,6 @@ type CardInfo = auto class - public + public cardNumber: string; cv: integer; end; diff --git a/TestSuite/patterns/patterns-simple-tuple.pas b/TestSuite/patterns/patterns-simple-tuple.pas index f7a0cd125..6a59f2d67 100644 --- a/TestSuite/patterns/patterns-simple-tuple.pas +++ b/TestSuite/patterns/patterns-simple-tuple.pas @@ -2,6 +2,6 @@ match (1, 2, 'string') with (1, _, 'str'): assert(false); (1, _, 'strin'): assert(false); - (_,_,'string'): ; + (_, _, 'string'): ; end; end. \ No newline at end of file diff --git a/TestSuite/patterns/patterns-type-matching1.pas b/TestSuite/patterns/patterns-type-matching1.pas index afe850574..ecb2fe5d7 100644 --- a/TestSuite/patterns/patterns-type-matching1.pas +++ b/TestSuite/patterns/patterns-type-matching1.pas @@ -1,6 +1,6 @@ type CardInfo = auto class - public + public cardNumber: string; cv: integer; end; @@ -29,7 +29,7 @@ begin var a := new Person('Вася', 11, new CardInfo('12345678', 324)); // Расширенный is - if a is Person('Вася', var age, CardInfo(_, var cv)) then assert((age = 11) and (cv = 324)); + if a is Person(_, var age, CardInfo(_, var cv)) then assert((age = 11) and (cv = 324)); // match .. with match a with