Comments (7)
#lang peg
braceLanguage <- item+;
item <- braceExpansion / simpleItem;
braceExpansion <- '{' braceLanguage (',' braceLanguage)* ','? '}';
simpleItem <- [^{,}]+;
this works on that example
from racket-peg.
#lang peg
(struct seq (a b) #:transparent);
(struct brace (l) #:transparent);
(struct idt (a) #:transparent);
braceLanguage <- v:item+ -> (let loop ((l v))
(if (null? (cdr l))
(car l)
(loop (cons (seq (car l) (cadr l)) (cddr l)))));
item <- braceExpansion / simpleItem;
braceExpansion <- ~'{' l:(braceLanguage (~',' braceLanguage)* ~(','?)) '}' -> l;
simpleItem <- v:[^{,}]+ -> (idt v);
with semantic actions
from racket-peg.
With semantic actions, we really need to use this (a . (b . c))
thing? Is not simpler to create structs?
from racket-peg.
#lang peg (struct seq (a b) #:transparent); (struct brace (l) #:transparent); (struct idt (a) #:transparent); braceLanguage <- v:item+ -> (let loop ((l v)) (if (null? (cdr l)) (car l) (loop (cons (seq (car l) (cadr l)) (cddr l))))); item <- braceExpansion / simpleItem; braceExpansion <- ~'{' l:(braceLanguage (~',' braceLanguage)* ~(','?)) '}' -> l; simpleItem <- v:[^{,}]+ -> (idt v);
with semantic actions
To me, the answer from this grammar is not confusing. (list a b)
represent {a,b}
,
(seq a b)
represent ab
where a and b are not both simpleItems
from racket-peg.
Whats some precise metrics of what you want?
from racket-peg.
I'm happy with this solution:
#lang racket
;; list-monad.rkt
(provide liftm2 sequence
concatenate string-concatenate)
(define (concatenate lists)
(apply append lists))
(define (string-concatenate strings)
(apply string-append strings))
(define (>>= m f)
(apply append (map f m)))
(define (liftm2 f a b)
(>>= a (lambda (x) (>>= b (lambda (y) (list (f x y)))))))
(define (sequence lsts)
(if (null? lsts)
(list '())
(liftm2 cons (car lsts) (sequence (cdr lsts)))))
#lang peg
(require "list-monad.rkt");
text <- str:[^{},]+ -> (list str);
items <- xs:(text / choice)+ -> (map string-concatenate (sequence xs));
choice <- '{' x:items xs:(~',' items)* '}' -> (concatenate (cons x xs));
from racket-peg.
Cool. Using monads
from racket-peg.
Related Issues (20)
- unicode safe HOT 5
- support to "..." HOT 3
- add quantifier repetition HOT 18
- Add verbose option on peg HOT 1
- Next milestone and Relase HOT 2
- Implement a extension of PEG HOT 2
- Can PEG results objects become more predictable?
- Parse Failure HOT 1
- document pegvm semantics HOT 7
- change (? e) to (or e (epsilon)) HOT 1
- No way to use bake from PEG language. HOT 1
- implement memorization of results HOT 1
- benchmark this
- Overriding/adding alternatives to rules from imported modules HOT 2
- 'capture' should be 'name' in reference HOT 1
- Release under LGPLv3+
- Bug with context-sensitive grammar HOT 1
- Precise defitition of the peg syntax? HOT 2
- source-location of tags
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from racket-peg.