File: calculator.peg

package info (click to toggle)
golang-github-pointlander-peg 1.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 376 kB
  • sloc: makefile: 44
file content (33 lines) | stat: -rw-r--r-- 885 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Copyright 2010 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

package main

type Calculator Peg {
 Expression
}

e <- sp e1 !.
e1 <- e2 ( add e2 { p.AddOperator(TypeAdd) }
         / minus e2 { p.AddOperator(TypeSubtract) }
         )*
e2 <- e3 ( multiply e3 { p.AddOperator(TypeMultiply) }
         / divide e3 { p.AddOperator(TypeDivide) }
         / modulus e3 { p.AddOperator(TypeModulus) }
         )*
e3 <- e4 ( exponentiation e4 { p.AddOperator(TypeExponentiation) }
         )*
e4 <- minus value { p.AddOperator(TypeNegation) }
    / value
value <- < [0-9]+ > sp { p.AddValue(buffer[begin:end]) }
       / open e1 close
add <- '+' sp
minus <- '-' sp
multiply <- '*' sp
divide <- '/' sp
modulus <- '%' sp
exponentiation <- '^' sp
open <- '(' sp
close <- ')' sp
sp <- ( ' ' / '\t' )*