Skip to content

Commit 79e82c4

Browse files
committed
Maybe finally working parser
1 parent 933e9ad commit 79e82c4

File tree

7 files changed

+81
-28
lines changed

7 files changed

+81
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
a.out.md
22
idk.pas
3+
idk2.pas

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/ProggerX/pabc-flowchart
33
go 1.22.7
44

55
require (
6-
github.com/Jemmic/go-pcre2 v0.0.0-20190111114109-bd52ad5f7098 // indirect
76
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
87
github.com/charmbracelet/lipgloss v0.10.0 // indirect
98
github.com/charmbracelet/log v0.4.0 // indirect
@@ -16,7 +15,6 @@ require (
1615
github.com/muesli/termenv v0.15.2 // indirect
1716
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
1817
github.com/rivo/uniseg v0.4.7 // indirect
19-
go.arsenm.dev/pcre v0.0.0-20220530205550-74594f6c8b0e // indirect
2018
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
2119
golang.org/x/sys v0.13.0 // indirect
2220
modernc.org/libc v1.16.8 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/Jemmic/go-pcre2 v0.0.0-20190111114109-bd52ad5f7098 h1:ZwFIi+5jGJWVrB2V4NvrEhIUy6uDkfnTtBsgj3HAImI=
2-
github.com/Jemmic/go-pcre2 v0.0.0-20190111114109-bd52ad5f7098/go.mod h1:c+8WT1L7lfohb4xMaa3yAV7nlYNepqc2ZV09/CU8R/U=
31
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
42
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
53
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
@@ -33,8 +31,6 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
3331
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
3432
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
3533
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
36-
go.arsenm.dev/pcre v0.0.0-20220530205550-74594f6c8b0e h1:4XwLmFDvAKt7ZvS3E3hD2R++0wr75fBUEvXkK9dLXzk=
37-
go.arsenm.dev/pcre v0.0.0-20220530205550-74594f6c8b0e/go.mod h1:c/E0D60A6rRLoDLh6mLUdFV9gxyth+CnXnqGHos2CAQ=
3834
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3935
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
4036
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=

idk2.pas

Lines changed: 0 additions & 5 deletions
This file was deleted.

internal/parser/block.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func parseBlock(id string, s string) (string, string, []string) {
1717
left := 0
1818
for i := 0; i < len(s); i++ {
1919
l := s[i:]
20-
if strings.HasPrefix(l, "begin ") {
20+
if strings.HasPrefix(l, "begin ") || strings.HasPrefix(l, "begin;") {
2121
scope++
2222
} else if strings.HasPrefix(l, "end") {
2323
scope--
@@ -27,6 +27,14 @@ func parseBlock(id string, s string) (string, string, []string) {
2727
left = i + 1
2828
}
2929
}
30+
if s[len(s)-1] != ';' {
31+
var i int
32+
for i = len(s) - 1; s[i] != ';'; i-- {
33+
}
34+
last := s[i+2:]
35+
log.Debug("BLOCK", "id", id, "last_op", last)
36+
ops = append(ops, last)
37+
}
3038
log.Debug("BLOCK", "id", id, "len(ops)", len(ops))
3139
prev_eid := bid
3240
for i := 0; i < len(ops); i++ {

internal/parser/if.go

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,44 @@ package parser
33
import (
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+
1042
func 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

2759
func 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"))

internal/parser/parser.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@ import (
77
"strings"
88

99
"github.com/charmbracelet/log"
10-
"go.arsenm.dev/pcre"
1110
)
1211

1312
func parseOperator(id string, s string) (string, string, []string) {
14-
rx_block := regexp.MustCompile(`^begin +(.+) +end`)
15-
// ^if +(.+?) +then +((begin *)*.*?(if.*?else)*.*?( *end)*) +else +(.+)
16-
rx_if_else := pcre.MustCompile(`^(?=.*if)(?=.*else).*`)
13+
s = strings.TrimSpace(s)
14+
rx_block := regexp.MustCompile(`^begin;* +(.+) +end`)
1715
rx_if := regexp.MustCompile(`^if +(.+) +then +(.+)`)
1816
rx_assign := regexp.MustCompile(`^(\w+) +(\+|-|\*|\/|:)= +(.+)`)
19-
rx_for := regexp.MustCompile(`^for +(var +)?(.+) +:= +(.+) +(to|downto) +(.+?)( +step +(.+))? +do +(.+)`)
17+
rx_for := regexp.MustCompile(`^for +(var +)?(.+) *:= *(.+) +(to|downto) +(.+?)( +step +(.+))? +do +(.+)`)
2018
rx_read_write := regexp.MustCompile(`^(write|read)(ln)?(\((.*)\))?`)
2119
rx_while := regexp.MustCompile(`^while +(.+) +(.+)`)
2220
is_block := rx_block.MatchString(s)
23-
is_if_else := rx_if_else.MatchString(s)
21+
is_if_else := detectIfElse(s)
2422
is_if := rx_if.MatchString(s)
2523
is_assign := rx_assign.MatchString(s)
2624
is_for := rx_for.MatchString(s)

0 commit comments

Comments
 (0)