Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
demizer committed Feb 6, 2014
0 parents commit 41eaa83
Show file tree
Hide file tree
Showing 15 changed files with 4,239 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!*
*.test
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (C) 2014 The go-rst Authors

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
================================
go-rst - reStructuredText for Go
================================

A reStructuredText parser written in Go.

============
Contributors
============

Jesus Alvarez
20 changes: 20 additions & 0 deletions document.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// go-rst - A reStructuredText parser for Go
// 2014 (c) The go-rst Authors
// MIT Licensed. See LICENSE for details.
package rst

import (
"github.com/demizer/go-rst/parse"
)

type Document struct {
name string
*parse.Tree
}

func New(name string) *Document {

return &Document{
name: name,
}
}
4 changes: 4 additions & 0 deletions document_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// go-rst - A reStructuredText parser for Go
// 2014 (c) The go-rst Authors
// MIT Licensed. See LICENSE for details.
package rst
82 changes: 82 additions & 0 deletions parse/lex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// go-rst - A reStructuredText parser for Go
// 2014 (c) The go-rst Authors
// MIT Licensed. See LICENSE for details.
package parse

type itemElements int

const (
itemEOF itemElements = iota
itemBodyParagraph
itemBodyText
itemBodyInlineEmphasis
itemBodyInlineStrong
itemBodyInlineInterpreted
itemBodyInlineInternalTarget
itemBodyInlineFootnote
itemBodyInlineHyperLink
itemBodyBulletList
itemBodyEnumeratedList
itemBodyDefinitionList
itemBodyFieldList
itemBodyBibliographicField
itemBodyOptionList
itemBodyLiteralBlock
itemBodyLiteralBlockIndented
itemBodyQuotedBlock
)

type stateFn func(*lexer) stateFn

type item struct {
typ itemElements
pos Pos
val string
}

type lexer struct {
name string
input string
state stateFn
pos Pos
start Pos
width Pos
lastPos Pos
items chan item
}

func lex(name, input string) *lexer {
l := &lexer{
name: name,
input: input,
items: make(chan item),
}
go l.run()
return l
}

func (l *lexer) run() {
for l.state = lexText; l.state != nil; {
l.state = l.state(l)
}
}

func lexText(l *lexer) stateFn {
// for {
// if strings.HasPrefix(l.input[l.pos:], l.leftDelim) {
// if l.pos > l.start {
// l.emit(itemText)
// }
// return lexLeftDelim
// }
// if l.next() == eof {
// break
// }
// }
// // Correctly reached EOF.
// if l.pos > l.start {
// l.emit(itemText)
// }
// l.emit(itemEOF)
return nil
}
75 changes: 75 additions & 0 deletions parse/lex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// go-rst - A reStructuredText parser for Go
// 2014 (c) The go-rst Authors
// MIT Licensed. See LICENSE for details.
package parse

import (
"bufio"
"fmt"
"os"
"strings"
"testing"
"encoding/json"
)

type lexTest struct {
description []byte
input []byte
expectJson []byte
}

func parseTestData(filepath string) ([]lexTest, error) {
testData, err := os.Open(filepath)
defer testData.Close()
if err != nil {
return nil, err
}
var lexTests []lexTest
var curTest lexTest
var buffer []byte
scanner := bufio.NewScanner(testData)
for scanner.Scan() {
switch scanner.Text() {
case "#description":
if len(buffer) > 0 {
curTest.expectJson = buffer
lexTests = append(lexTests, curTest)
curTest = lexTest{}
} else {
curTest = lexTest{}
}
buffer = nil
case "#data":
curTest.description = buffer
buffer = nil
case "#tree":
curTest.input = buffer
buffer = nil
default:
if len(scanner.Text()) == 0 ||
strings.TrimLeft(scanner.Text(), " ")[0] == '#' {
continue
}
buffer = append(buffer, scanner.Bytes()...)
}
}
return lexTests, nil
}

func TestSection(t *testing.T) {
lexTests, err := parseTestData("../testdata/test_section_headers.dat")
if err != nil {
t.FailNow()
}
// for _, iTest := range lexTests {
// fmt.Printf("DESCRIPTION: %#v\n", iTest.description)
// fmt.Printf("INPUT: %#v\n", iTest.input)
// fmt.Printf("JSON: %#v\n", iTest.expectJson)
// }
var i interface{}
err = json.Unmarshal(lexTests[0].expectJson, &i)
if err != nil {
t.Errorf("JSON Error: %s, IN: %s", err, lexTests[0])
}
fmt.Printf("%#v\n", i)
}
6 changes: 6 additions & 0 deletions parse/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// go-rst - A reStructuredText parser for Go
// 2014 (c) The go-rst Authors
// MIT Licensed. See LICENSE for details.
package parse

type Pos int
1 change: 1 addition & 0 deletions parse/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package parse
21 changes: 21 additions & 0 deletions parse/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// go-rst - A reStructuredText parser for Go
// 2014 (c) The go-rst Authors
// MIT Licensed. See LICENSE for details.
package parse

type Tree struct {
Name string
text string
// Document *ListNode
lex *lexer
}

func New(name string) *Tree {
return &Tree{
Name: name,
}
}

func (t *Tree) Parse(text string) (tree *Tree, err error) {
return nil, nil
}
4 changes: 4 additions & 0 deletions parse/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// go-rst - A reStructuredText parser for Go
// 2014 (c) The go-rst Authors
// MIT Licensed. See LICENSE for details.
package parse
Loading

0 comments on commit 41eaa83

Please sign in to comment.