From 071bf92c4fa431a2d7c7e8561c4eb85ea13607d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Teichgr=C3=A4ber?= Date: Wed, 24 Nov 2010 19:36:10 +0100 Subject: [PATCH] use a single yyParser instance only, and reuse initialized parser --- markdown.go | 19 ++++++++++--------- parser.leg | 2 ++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/markdown.go b/markdown.go index cc32873..81a2528 100644 --- a/markdown.go +++ b/markdown.go @@ -22,6 +22,7 @@ package markdown import ( "strings" "bytes" + "log" ) // Markdown Extensions: @@ -37,6 +38,10 @@ func Parse(text string, extFlags int) *Doc { d := new(Doc) d.syntaxExtensions = extFlags + d.parser = new(yyParser) + d.parser.Doc = d + d.parser.Init() + s := preformat(text) d.parseRule(ruleReferences, s) @@ -49,21 +54,17 @@ func Parse(text string, extFlags int) *Doc { } func (d *Doc) parseRule(rule int, s string) { - m := new(yyParser) - m.Doc = d - m.Init() - m.Buffer = s + m := d.parser + if m.ResetBuffer(s) != "" { + log.Exitf("Buffer not empty") + } if !m.Parse(rule) { m.PrintError() } } func (d *Doc) parseMarkdown(text string) *element { - m := new(yyParser) - m.Doc = d - m.Init() - m.Buffer = text - m.Parse(ruleDoc) + d.parseRule(ruleDoc, text) return d.tree } diff --git a/parser.leg b/parser.leg index 3ea586f..4d38a26 100644 --- a/parser.leg +++ b/parser.leg @@ -87,6 +87,8 @@ const ( ) type Doc struct { + parser *yyParser + tree *element /* Results of parse. */ references *element /* List of link references found. */ notes *element /* List of footnotes found. */