parser

package
Version: v0.0.0-...-35d07ea Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package parser implements a parser for mexdown source. It takes in an io.Reader as input and outputs an *ast.File.

It is the responsibility of the generator to parse commands attached to directive nodes.

The parser adheres to the following grammar for mexdown source files:

unicode_char = /* an arbitrary Unicode code point except newline */ .
newline      = /* the Unicode code point U+000A */ .
tab          = /* the Unicode code point U+0009 */ .
octothorpe   = /* the Unicode code point U+0023 */ .
backtick     = /* the Unicode code point U+0060 */ .
hyphen       = /* the Unicode code point U+002D */ .
asterisk     = /* the Unicode code point U+002A */ .
lbrack       = /* the Unicode code point U+005B */ .
rbrack       = /* the Unicode code point U+005D */ .
lparen       = /* the Unicode code point U+0028 */ .
rparen       = /* the Unicode code point U+0029 */ .
underscore   = /* the Unicode code point U+005F */ .
colon        = /* the Unicode code point U+003A */ .

citation = lbrack text rbrack colon string .
paragraph = text .
list_item = { tab } hyphen [ lbrack text rbrack ] text .
list = { list_item newline } [ list_item ] .
string = { unicode_char | newline } .
command = unicode_char { unicode_char } .
dirbody = backtick dirbody backtick | [ command ] newline string .
directive = backtick backtick backtick dirbody backtick backtick backtick .
text = unicode_char { unicode_char } |
       lbrack text rbrack lparen text rparen |
       asterisk text asterisk |
       asterisk asterisk text asterisk asterisk |
       underscore text underscore |
       hyphen hyphen text hyphen hyphen |
       backtick text backtick .
header = octothorpe { octothorpe } text .
statement = header | directive | list | paragraph | citation .
source_file = { statement [ newline ] [ newline ] } .

In the relevant context, the following characters are escaped (in Go syntax):

'\\', '#', '`', '-', '*', '[', ']', '(', ')', '_'

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustParse

func MustParse(src io.Reader) (file *ast.File)

MustParse is like Parse but panics if the source cannot be parsed.

Example
package main

import (
	"fmt"
	"strings"

	"akhil.cc/mexdown/ast"
	"akhil.cc/mexdown/parser"
)

func main() {
	src := `# Favorite Hobbits
- Frodo
- Samwise
- Bilbo
- Merry
- Pippin
`
	file := parser.MustParse(strings.NewReader(src))
	for _, stmt := range file.List {
		switch t := stmt.(type) {
		case *ast.Header:
			fmt.Println(t.Text.Body)
		case *ast.List:
			for _, item := range t.Items {
				fmt.Println(item.Text.Body)
			}
		}
	}
}
Output:

Favorite Hobbits
 Frodo
 Samwise
 Bilbo
 Merry
 Pippin

func Parse

func Parse(src io.Reader) (f *ast.File, err error)

Parse parses the source and if successful, returns its corresponding AST structure. A generator can be used to transform the returned AST into another format.

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL