@@ -3,12 +3,44 @@ package parser
33import (
44 "fmt"
55 "regexp"
6+ "strings"
67
78 "github.com/charmbracelet/log"
89)
910
11+ func detectIfElse (s string ) bool {
12+ rx_common_sense := regexp .MustCompile (`^if +(.+?) +then +(.+) +else +(.+)` )
13+ if ! rx_common_sense .MatchString (s ) {
14+ return false
15+ }
16+ rx := regexp .MustCompile (`^if +(.+?) +then` )
17+ old_s := s
18+ s = rx .ReplaceAllString (s , "" )
19+ s = strings .TrimSpace (s )
20+
21+ scope := 0
22+ scope_if := 0
23+ for i := 0 ; i < len (s ); i ++ {
24+ l := s [i :]
25+ if (strings .HasPrefix (l , "begin " ) || strings .HasPrefix (l , "begin;" )) && (i == 0 || s [i - 1 ] == ' ' ) {
26+ scope ++
27+ } else if strings .HasPrefix (l , " end" ) {
28+ scope --
29+ } else if strings .HasPrefix (l , "if " ) && scope == 0 && (i == 0 || s [i - 1 ] == ' ' ) {
30+ scope_if ++
31+ } else if strings .HasPrefix (l , " else " ) && scope == 0 {
32+ if scope_if <= 0 {
33+ log .Debug ("DETECTED_IF_ELSE" , "s" , old_s )
34+ return true
35+ }
36+ scope_if --
37+ }
38+ }
39+ return false
40+ }
41+
1042func parseIf (id string , s string ) (string , string , []string ) {
11- rx := regexp .MustCompile (`if +(.+) +then +(.+)` )
43+ rx := regexp .MustCompile (`if +(.+? ) +then +(.+)` )
1244 cond := rx .FindStringSubmatch (s )[1 ]
1345 then := rx .FindStringSubmatch (s )[2 ]
1446 log .Debug ("IF" , "id" , id , "cond" , cond )
@@ -25,19 +57,44 @@ func parseIf(id string, s string) (string, string, []string) {
2557}
2658
2759func parseIfElse (id string , s string ) (string , string , []string ) {
28- rx := regexp .MustCompile (`^if + (.+?) + then +((begin *)*.*?(if.*?else)*.*?( *end)*) +else +(.+) ` )
60+ rx := regexp .MustCompile (`^if (.+?) then` )
2961 cond := rx .FindStringSubmatch (s )[1 ]
30- then := rx .FindStringSubmatch (s )[2 ]
31- els := rx .FindStringSubmatch (s )[6 ]
62+ s = rx .ReplaceAllString (s , "" )
63+
64+ scope := 0
65+ scope_if := 0
66+ var then_op , else_op string
67+ for i := 0 ; i < len (s ); i ++ {
68+ l := s [i :]
69+ if (strings .HasPrefix (l , "begin " ) || strings .HasPrefix (l , "begin;" )) && (i == 0 || s [i - 1 ] == ' ' ) {
70+ scope ++
71+ } else if strings .HasPrefix (l , " end" ) {
72+ scope --
73+ } else if strings .HasPrefix (l , "if " ) && scope == 0 && (i == 0 || s [i - 1 ] == ' ' ) {
74+ scope_if ++
75+ } else if strings .HasPrefix (l , " else " ) && scope == 0 {
76+ if scope_if > 0 {
77+ scope_if --
78+ } else {
79+ log .Debug ("delimeter" , "i" , i )
80+ then_op = s [1 :i ]
81+ else_op = s [i + 6 :]
82+ break
83+ }
84+ }
85+ }
86+
3287 log .Debug ("IF_ELSE" , "id" , id , "cond" , cond )
33- log .Debug ("IF_ELSE" , "id" , id , "then" , then )
34- log .Debug ("IF_ELSE" , "id" , id , "else" , els )
88+ log .Debug ("IF_ELSE" , "id" , id , "then_op" , then_op )
89+ log .Debug ("IF_ELSE" , "id" , id , "else_op" , else_op )
90+
91+ then_bid , then_eid , then_ops := parseOperator (id + "then" , then_op )
92+ else_bid , else_eid , else_ops := parseOperator (id + "else" , else_op )
93+
3594 o := []string {}
3695 o = append (o , fmt .Sprintf ("%s{\" %s ?\" }" , id , cond ))
37- then_bid , then_eid , then_r := parseOperator (id + "then" , then )
38- else_bid , else_eid , else_r := parseOperator (id + "else" , els )
39- o = append (o , then_r ... )
40- o = append (o , else_r ... )
96+ o = append (o , then_ops ... )
97+ o = append (o , else_ops ... )
4198 o = append (o , fmt .Sprintf ("%s-->|тогда|%s" , id , then_bid ))
4299 o = append (o , fmt .Sprintf ("%s-->|иначе|%s" , id , else_bid ))
43100 o = append (o , fmt .Sprintf ("%s-->%s" , else_eid , id + "end" ))
0 commit comments