zevv / npeg

PEGs for Nim, another take
MIT License
330 stars 22 forks source link

[Question] Parsing comments with two characters limits #48

Closed mantielero closed 1 year ago

mantielero commented 1 year ago

I am trying to parse comments with limits: (* and *).

I am trying the following without success:

import npeg

type
  Comment = string

let tmp = "(*This is a comment*)"  # Nested comments are not allowed

let parser = peg("comment", d: Comment):
  commentStart <- "(*"
  commentEnd <- "*)"
  #content <- *(1-commentEnd) 
  #comment <- commentStart * >content * commentEnd:
  comment <- "(*" * >(1-"*)") * "*)":
    d = $1

var comm:Comment
#assert parser.match(tmp, comm).ok
echo parser.match(tmp, comm).captures

#echo comm

The sort of comments I could expect are like:

(* This is an exmaple *)
(* This is 
 * an example
 *)
(********)
(* Ok   *)
(********)
(*
  enum_v1 : enum_type00;
*)
zevv commented 1 year ago
import npeg

let tmp = "(*This is a comment*)"  # Nested comments are not allowed

let parser = peg "comment":
  comment <- "(*" * >*(1-"*)") * "*)"              

echo parser.match(tmp).captures                    

output:

@["This is a comment"]
mantielero commented 1 year ago

Thanks a lot