1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file contains the mechanism to "linkify" html source
// text containing EBNF sections (as found in go_spec.html).
// The result is the input source text with the EBNF sections
// modified such that identifiers are linked to the respective
// definitions.
package main
import (
"bytes"
"fmt"
"go/scanner"
"go/token"
"io"
"strings"
)
type ebnfParser struct {
out io.Writer // parser output
src []byte // parser source
scanner scanner.Scanner
prev int // offset of previous token
pos token.Position // token position
tok token.Token // one token look-ahead
lit []byte // token literal
}
func (p *ebnfParser) flush() {
p.out.Write(p.src[p.prev:p.pos.Offset])
p.prev = p.pos.Offset
}
func (p *ebnfParser) next() {
p.flush()
p.pos, p.tok, p.lit = p.scanner.Scan()
if p.tok.IsKeyword() {
// TODO Should keyword mapping always happen outside scanner?
// Or should there be a flag to scanner to enable keyword mapping?
p.tok = token.IDENT
}
}
func (p *ebnfParser) Error(pos token.Position, msg string) {
fmt.Fprintf(p.out, `<span class="alert">error: %s</span>`, msg)
}
func (p *ebnfParser) errorExpected(pos token.Position, msg string) {
msg = "expected " + msg
if pos.Offset == p.pos.Offset {
// the error happened at the current position;
// make the error message more specific
msg += ", found '" + p.tok.String() + "'"
if p.tok.IsLiteral() {
msg += " " + string(p.lit)
}
}
p.Error(pos, msg)
}
func (p *ebnfParser) expect(tok token.Token) token.Position {
pos := p.pos
if p.tok != tok {
p.errorExpected(pos, "'"+tok.String()+"'")
}
p.next() // make progress in any case
return pos
}
func (p *ebnfParser) parseIdentifier(def bool) {
name := string(p.lit)
p.expect(token.IDENT)
if def {
fmt.Fprintf(p.out, `<a id="%s">%s</a>`, name, name)
} else {
fmt.Fprintf(p.out, `<a href="#%s" class="noline">%s</a>`, name, name)
}
p.prev += len(name) // skip identifier when calling flush
}
func (p *ebnfParser) parseTerm() bool {
switch p.tok {
case token.IDENT:
p.parseIdentifier(false)
case token.STRING:
p.next()
if p.tok == token.ELLIPSIS {
p.next()
p.expect(token.STRING)
}
case token.LPAREN:
p.next()
p.parseExpression()
p.expect(token.RPAREN)
case token.LBRACK:
p.next()
p.parseExpression()
p.expect(token.RBRACK)
case token.LBRACE:
p.next()
p.parseExpression()
p.expect(token.RBRACE)
default:
return false
}
return true
}
func (p *ebnfParser) parseSequence() {
for p.parseTerm() {
}
}
func (p *ebnfParser) parseExpression() {
for {
p.parseSequence()
if p.tok != token.OR {
break
}
p.next()
}
}
func (p *ebnfParser) parseProduction() {
p.parseIdentifier(true)
p.expect(token.ASSIGN)
p.parseExpression()
p.expect(token.PERIOD)
}
func (p *ebnfParser) parse(out io.Writer, src []byte) {
// initialize ebnfParser
p.out = out
p.src = src
p.scanner.Init("", src, p, 0)
p.next() // initializes pos, tok, lit
// process source
for p.tok != token.EOF {
p.parseProduction()
}
p.flush()
}
// Markers around EBNF sections
var (
openTag = strings.Bytes(`<pre class="ebnf">`)
closeTag = strings.Bytes(`</pre>`)
)
func linkify(out io.Writer, src []byte) {
for len(src) > 0 {
n := len(src)
// i: beginning of EBNF text (or end of source)
i := bytes.Index(src, openTag)
if i < 0 {
i = n - len(openTag)
}
i += len(openTag)
// j: end of EBNF text (or end of source)
j := bytes.Index(src[i:n], closeTag) // close marker
if j < 0 {
j = n - i
}
j += i
// write text before EBNF
out.Write(src[0:i])
// parse and write EBNF
var p ebnfParser
p.parse(out, src[i:j])
// advance
src = src[j:n]
}
}
|