How should I bind? I's completely unclear to me.
import std/[streams, pegs]
let stream = newStringStream("""
0-0:96.13.0()
1-3:0.2.8(50)
0-0:1.0.0(221019115846S)
1-0:32.32.0(00000)
0-0:96.1.1(4530303737307730373236383936353232)
1-0:1.8.1(000865.964*kWh)
1-0:31.7.0(002*A)
0-0:96.14.0(0002)
1-0:99.97.0(1)(0-0:96.7.19)(220120193901W)(0000000000*s)
0-1:24.2.1(221019115500S)(00112.177*m3)
"""
)
#this works fine
#let p = peg"""
# {\d+ '-' \d+ ':' \d+ '.' \d+ '.' \d+}
# {'(' [0-9a-zA-Z:.*\-]* ')'}+
#"""
let p = peg"""
{orbi} {data}+
orbi <- \d+ '-' \d+ ':' \d+ '.' \d+ '.' \d+
data <- '(' [0-9a-zA-Z:.*\-]* ')'
"""
var line: string
while stream.readline(line):
if line =~ p:
for i, val in matches:
if val != "":
echo i," ", val
echo "\n"
Is there a good reason why you are not using npeg:
https://ssalewski.de/nimprogramming.html#_parsing_expression_grammars
That one has documentation and was working fine for me some years ago.
It works. It needs more refinement, that will come with time.
import std/[streams]
import npeg
let stream = newStringStream("""
0-0:96.13.0()
1-3:0.2.8(50)
0-0:1.0.0(221019115846S)
1-0:32.32.0(00000)
0-0:96.1.1(4530303737307730373236383936353232)
1-0:1.8.1(000865.964*kWh)
1-0:31.7.0(002*A)
0-0:96.14.0(0002)
1-0:99.97.0(1)(0-0:96.7.19)(220120193901W)(0000000000*s)
0-1:24.2.1(221019115500S)(00112.177*m3)
"""
)
let p = peg("obis"):
obis <- >obis_id * val
val <- +('(' * *(>timestamp | >intunit | >floatunit | >obis_id | >+Digit) * ')')
obis_id <- (+Digit * "-" * +Digit * ':' * +Digit * '.' * +Digit * '.' * +Digit)
timestamp <- {'0'..'9'}[12] * {'W','S'}
intunit <- +Digit * '*' * +Alpha
floatunit <- flt * '*' * +Alpha * *Digit
flt <- +Digit * '.' * +Digit
#alldata <- *{'A'..'Z','a'..'z','0'..'9','*',':','.','-'}
var line: string
while stream.readline(line):
let m = p.match(line)
if m.ok:
echo m.captures
This is for peg definition.
#this also works well
let p = peg"""
exp <- {orbi} {data}+
orbi <- \d+ '-' \d+ ':' \d+ '.' \d+ '.' \d+
data <- '(' [0-9a-zA-Z:.*\-]* ')'
"""