Skip to content

Commit 269ef1a

Browse files
authored
Merge pull request #33 from Tsukuba-Programming-Lab/#19_ImplExample
#19 example 追加
2 parents f63c957 + 429c62e commit 269ef1a

File tree

13 files changed

+157
-73
lines changed

13 files changed

+157
-73
lines changed

README.md

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
# Copager2(仮)
22

3-
## Bin
3+
## 記述例([app/src/Arithmetic.hs](./app/Arithmetic.hs)
44

5-
- [Main](./app) : メインプログラム
5+
```haskell
6+
main :: IO ()
7+
main = do
8+
input <- getLine
9+
result <- runProcessor mkProcessor input
10+
case result of
11+
Right sexp -> print sexp
12+
Left err -> putStrLn $ "error: " ++ show err
13+
14+
mkProcessor :: ProcessorT IO ArithmeticToken ArithmeticRule (SExp _ _)
15+
mkProcessor = ProcessorT $ do
16+
token <- lex
17+
events <- parse token
18+
consume events
19+
20+
```
621

722
## Library
823

@@ -12,40 +27,14 @@
1227

1328
## Example
1429

15-
### 記述例([app/Main.hs](./app/Main.hs)
30+
### 普通のパーサ
1631

17-
```haskell
18-
main :: IO ()
19-
main = do
20-
input <- getLine
21-
result <- runProcessor mkProcessor input
22-
case result of
23-
Left err -> putStrLn $ "error: " ++ show err
24-
Right sexp -> fmtPrint sexp
25-
26-
mkProcessor :: ProcessorT IO ArithmeticToken ArithmeticRule () (SExp _ _)
27-
mkProcessor = ProcessorT setup process where
28-
setup _ = (mkSLR1 Addition, empty)
29-
process = do
30-
token <- lex
31-
events <- parse token
32-
consume events
33-
```
32+
- [lang-arithmetic](./app/lang/src/Arithmetic.hs) : 四則演算
33+
- [lang-json](./app/lang/src/json.hs) : JSON
34+
- [lang-pl0](./app/lang/src/Pl0.hs) : Pl/0
35+
- [lang-xml](./app/lang/src/Xml.hs) : Xml
3436

35-
### 実行
37+
### エラーハンドリング
3638

37-
```
38-
$ cabal run main
39-
(10 + 20) * 30
40-
- Addition
41-
- Multiplication
42-
- Multiplication
43-
- Number
44-
- Addition
45-
- Addition
46-
- Multiplication
47-
- Number: 10
48-
- Multiplication
49-
- Number: 20
50-
- Number: 30
51-
```
39+
- [errh-panicmode](./app/errh/src/PanicMode.hs) : パニックモード
40+
- [errh-corchuelo](./app/errh/src/Corchuelo.hs) : Corchuelo (※)

app/errh_corchuelo/errh_corchuelo.cabal renamed to app/errh/errh.cabal

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
cabal-version: 3.0
2-
name: errh-corchuelo
2+
name: errh
33
version: 0.1.0.0
44
author: NakagamiYuta
55
maintainer: guguru0014@gmail.com
66
build-type: Simple
77

8+
executable errh-panicmode
9+
main-is: PanicMode.hs
10+
hs-source-dirs: src
11+
build-depends:
12+
base,
13+
mtl,
14+
copager2,
15+
language,
16+
err-handling,
17+
818
executable errh-corchuelo
9-
main-is: Main.hs
19+
main-is: corchuelo.hs
1020
hs-source-dirs: src
1121
build-depends:
1222
base,
File renamed without changes.
File renamed without changes.

app/errh_panicmode/errh_panicmode.cabal

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

app/lang/lang.cabal

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
cabal-version: 3.0
2+
name: lang
3+
version: 0.1.0.0
4+
author: NakagamiYuta
5+
maintainer: guguru0014@gmail.com
6+
build-type: Simple
7+
8+
executable lang-arithmetic
9+
main-is: Arithmetic.hs
10+
hs-source-dirs: src
11+
build-depends:
12+
base,
13+
mtl,
14+
copager2,
15+
language,
16+
17+
executable lang-json
18+
main-is: Json.hs
19+
hs-source-dirs: src
20+
build-depends:
21+
base,
22+
mtl,
23+
copager2,
24+
language,
25+
26+
executable lang-pl0
27+
main-is: Pl0.hs
28+
hs-source-dirs: src
29+
build-depends:
30+
base,
31+
mtl,
32+
copager2,
33+
language,
34+
35+
executable lang-xml
36+
main-is: Xml.hs
37+
hs-source-dirs: src
38+
build-depends:
39+
base,
40+
mtl,
41+
copager2,
42+
language,
File renamed without changes.

app/lang/src/Json.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{-# LANGUAGE PartialTypeSignatures #-}
2+
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
3+
4+
import Prelude hiding (lex)
5+
6+
import Copager2.Prelude
7+
import Copager2.Lex.RegexLex (lex)
8+
import Copager2.Parse.LR.Common.Driver (parse)
9+
import Copager2.IR.SExp (SExp, consume)
10+
11+
import Language.Json (JsonToken, JsonRule)
12+
13+
main :: IO ()
14+
main = do
15+
input <- getLine
16+
result <- runProcessor mkProcessor input
17+
case result of
18+
Right sexp -> print sexp
19+
Left err -> putStrLn $ "error: " ++ show err
20+
21+
mkProcessor :: ProcessorT IO JsonToken JsonRule (SExp _ _)
22+
mkProcessor = ProcessorT $ do
23+
token <- lex
24+
events <- parse token
25+
consume events

app/lang/src/Pl0.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{-# LANGUAGE PartialTypeSignatures #-}
2+
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
3+
4+
import Prelude hiding (lex)
5+
6+
import Copager2.Prelude
7+
import Copager2.Lex.RegexLex (lex)
8+
import Copager2.Parse.LR.Common.Driver (parse)
9+
import Copager2.IR.SExp (SExp, consume)
10+
11+
import Language.Pl0 (Pl0Token, Pl0Rule)
12+
13+
main :: IO ()
14+
main = do
15+
input <- getLine
16+
result <- runProcessor mkProcessor input
17+
case result of
18+
Right sexp -> print sexp
19+
Left err -> putStrLn $ "error: " ++ show err
20+
21+
mkProcessor :: ProcessorT IO Pl0Token Pl0Rule (SExp _ _)
22+
mkProcessor = ProcessorT $ do
23+
token <- lex
24+
events <- parse token
25+
consume events

app/lang/src/Xml.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{-# LANGUAGE PartialTypeSignatures #-}
2+
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
3+
4+
import Prelude hiding (lex)
5+
6+
import Copager2.Prelude
7+
import Copager2.Lex.RegexLex (lex)
8+
import Copager2.Parse.LR.Common.Driver (parse)
9+
import Copager2.IR.SExp (SExp, consume)
10+
11+
import Language.Xml (XmlToken, XmlRule)
12+
13+
main :: IO ()
14+
main = do
15+
input <- getLine
16+
result <- runProcessor mkProcessor input
17+
case result of
18+
Right sexp -> print sexp
19+
Left err -> putStrLn $ "error: " ++ show err
20+
21+
mkProcessor :: ProcessorT IO XmlToken XmlRule (SExp _ _)
22+
mkProcessor = ProcessorT $ do
23+
token <- lex
24+
events <- parse token
25+
consume events

0 commit comments

Comments
 (0)