From aebdd5c60c4069c3268cf1161c46a0fd8e4f4172 Mon Sep 17 00:00:00 2001 From: Simon Symeonidis Date: Sun, 18 Apr 2021 15:12:57 -0400 Subject: [PATCH] [tinystory] Add minor work for first revision of project --- tinystory/README.md | 5 +++ tinystory/lib/parser.go | 4 ++ tinystory/lib/server.go | 2 + tinystory/lib/session.go | 2 + tinystory/lib/tinystory_parser.go | 63 ++++++++++++++++++++++++++++++ tinystory/main.go | 6 +++ tinystory/stories/simple.json | 36 ++++++++--------- tinystory/stories/simple.tinystory | 40 +++++++++++++++++++ 8 files changed, 140 insertions(+), 18 deletions(-) create mode 100644 tinystory/lib/tinystory_parser.go create mode 100644 tinystory/stories/simple.tinystory diff --git a/tinystory/README.md b/tinystory/README.md index 43886ad..f4256b2 100644 --- a/tinystory/README.md +++ b/tinystory/README.md @@ -30,3 +30,8 @@ Run `make` in the root directory. Then you can run the binary in this directory. You just need to pass the proper flags to the binary to point to a story repository, and an assets directory (with the html templates). + +# todo + +Since we're dealing with graphs, might be nice to have some checks to +make sure that certain nodes are not orphaned. diff --git a/tinystory/lib/parser.go b/tinystory/lib/parser.go index 26029f3..59d608e 100644 --- a/tinystory/lib/parser.go +++ b/tinystory/lib/parser.go @@ -1,3 +1,7 @@ +/** + * A very simple parser that relies on stories that are written in + * json + */ package tinystory import ( diff --git a/tinystory/lib/server.go b/tinystory/lib/server.go index 2f6f89a..67651ae 100644 --- a/tinystory/lib/server.go +++ b/tinystory/lib/server.go @@ -114,6 +114,8 @@ func (s *Server) HandleStory(w http.ResponseWriter, r *http.Request) { nodeIndex = maybeNodeIndex } + // TODO: make a safe index check here with min(len(documents), actual) + responseData := struct { Title string Authors []string diff --git a/tinystory/lib/session.go b/tinystory/lib/session.go index ae7936d..64461e2 100644 --- a/tinystory/lib/session.go +++ b/tinystory/lib/session.go @@ -5,6 +5,8 @@ type Session struct { Port string Repository string Assets string + + ExperimentalParser string } func MakeDefaultSession() *Session { diff --git a/tinystory/lib/tinystory_parser.go b/tinystory/lib/tinystory_parser.go new file mode 100644 index 0000000..f153a82 --- /dev/null +++ b/tinystory/lib/tinystory_parser.go @@ -0,0 +1,63 @@ +/** + * A more experimental parser for the tinystory project. I'm mostly + * leveraging around generating json for now. Maybe we can scrap that + * in the future for our very own parser... + */ +package tinystory + +import ( + "errors" + "io" + "os" +) + +type TokenTypeEnum uint64 + +const ( + TokenKeyword TokenTypeEnum = iota + TokenWord + TokenWhitespace + TokenNewline + TokenNumber +) + +// TODO: this will eventually be used +// nolint +var terminals = []string{ + "TITLE", + "COMMENTS", + "AUTHORS", + "CHOICE", + "FRAGMENT", + "ENDFRAGMENT", + "GOTO", +} + +type Token struct { + Type TokenTypeEnum + Value string + LineNumber uint64 +} + +func ParseTinyStoryFormat(path string) (*Document, error) { + fs, err := os.Open(path) + if err != nil { + return nil, err + } + doc, err := ParseTinystoryFormat(fs) + return doc, err +} + +func ParseTinystoryFormat(reader io.ReadCloser) (*Document, error) { + var b [1]byte + + for { + _, err := reader.Read(b[:]) + if errors.Is(err, io.EOF) { + break + } + } + + defer reader.Close() + return nil, nil +} diff --git a/tinystory/main.go b/tinystory/main.go index 5ade259..a67de54 100644 --- a/tinystory/main.go +++ b/tinystory/main.go @@ -13,6 +13,7 @@ func makeFlags(sess *tinystory.Session) { flag.StringVar(&sess.Port, "port", sess.Port, "specify port to bind server") flag.StringVar(&sess.Repository, "repository", sess.Repository, "specify story repository") flag.StringVar(&sess.Assets, "assets", sess.Assets, "specify the assets root path") + flag.StringVar(&sess.ExperimentalParser, "experimental-parser", sess.ExperimentalParser, "use experimental parser") flag.Parse() } @@ -20,6 +21,11 @@ func main() { sess := tinystory.MakeDefaultSession() makeFlags(sess) + if sess.ExperimentalParser != "" { + // TODO experimental for now + _, _ = tinystory.ParseTinyStoryFormat(sess.ExperimentalParser) + } + // TODO: there should be a less bleedy initialization here docs, err := tinystory.ParseAllInDir(sess.Repository) if err != nil { diff --git a/tinystory/stories/simple.json b/tinystory/stories/simple.json index 69e4fbb..2478cfc 100644 --- a/tinystory/stories/simple.json +++ b/tinystory/stories/simple.json @@ -1,32 +1,32 @@ { - "title": "the platypus war: part CXXII", - "authors": ["jon doe", "jon smith", "jane doe"], - "website": "https://github.com/psyomn/cacophagy", + "title": "the platypus war: part CXXII", + "authors": ["jon doe", "jon smith", "jane doe"], + "website": "https://github.com/psyomn/cacophagy", - "dev_comments": "maybe we can do something better in the future, like provide an editor to create stories easier", + "dev_comments": "maybe we can do something better in the future, like provide an editor to create stories easier", - "story": [ - [0, - "you wake up in a dark room. you see a platypus. what do you do?", - [["choose to greet the platypus", 1], - ["choose to ignore the platypus", 1], - ["choose to look at the ceiling", 1]] - ], + "story": [ + [0, + "you wake up in a dark room. you see a platypus. what do you do?", + [["choose to greet the platypus", 1], + ["choose to ignore the platypus", 1], + ["choose to look at the ceiling", 1]] + ], - [1, + [1, "the platypus heeds your greeting, and tells you about a terrible war between the platypii and gingerbread men\nHe asks for your help.", - [["help platypus fight gingerbread men", 2], - ["decline to help the platypus", 3]] - ], + [["help platypus fight gingerbread men", 2], + ["decline to help the platypus", 3]] + ], - [2, + [2, "you help the platypus by travelling far lands, finding where the gingerbread men infestation is, and drown their whole civilizatio in mapple syrup. the battle is won!\nThe platypus thanks you, and you go home.", [] - ], + ], [3, "you decline to help the platypus because you have better things to do. gingerbread men erupt from the ground, grab the platypus and eat it alive right in front of you. you monster.", [] ] - ] + ] } diff --git a/tinystory/stories/simple.tinystory b/tinystory/stories/simple.tinystory new file mode 100644 index 0000000..00ff1e3 --- /dev/null +++ b/tinystory/stories/simple.tinystory @@ -0,0 +1,40 @@ +TITLE +The platypus war: part CXXII; + +AUTHORS +jon doe; +jon smith; +jane doeson; +jake andbake; + + +COMMENTS +maybe we can do something better in the future, like provide +an editor to create stories easier. For now maybe the + +FRAGMENT 0 +you wake up in a dark room. you see a platypus. what do you do? + GOTO 1 choose to greet the platypus + GOTO 1 choose to ignore the platypus + GOTO 1 choose to look at the ceiling +ENDFRAGMENT + +FRAGMENT 1 +the platypus heeds your greeting, and tells you about a terrible war +between the platypii and gingerbread men. He asks for your help. + GOTO 2 help platypus fight gingerbread men + GOTO 3 decline to help the platypus +ENDFRAGMENT + +FRAGMENT 2 +you help the platypus by travelling far lands, finding where the +gingerbread men infestation is, and drown their whole civilizatio in +mapple syrup. the battle is won! The platypus thanks you, and you go +home. +ENDFRAGMENT + +FRAGMENT 3 +you decline to help the platypus because you have better things to +do. gingerbread men erupt from the ground, grab the platypus and eat +it alive right in front of you. you monster. +ENDFRAGMENT \ No newline at end of file