maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

  1/*
  2Maddy Mail Server - Composable all-in-one email server.
  3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
  4
  5This program is free software: you can redistribute it and/or modify
  6it under the terms of the GNU General Public License as published by
  7the Free Software Foundation, either version 3 of the License, or
  8(at your option) any later version.
  9
 10This program is distributed in the hope that it will be useful,
 11but WITHOUT ANY WARRANTY; without even the implied warranty of
 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13GNU General Public License for more details.
 14
 15You should have received a copy of the GNU General Public License
 16along with this program.  If not, see <https://www.gnu.org/licenses/>.
 17*/
 18
 19// Copyright 2015 Light Code Labs, LLC
 20//
 21// Licensed under the Apache License, Version 2.0 (the "License");
 22// you may not use this file except in compliance with the License.
 23// You may obtain a copy of the License at
 24//
 25//     http://www.apache.org/licenses/LICENSE-2.0
 26//
 27// Unless required by applicable law or agreed to in writing, software
 28// distributed under the License is distributed on an "AS IS" BASIS,
 29// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 30// See the License for the specific language governing permissions and
 31// limitations under the License.
 32
 33package lexer
 34
 35import (
 36	"errors"
 37	"fmt"
 38	"io"
 39	"strings"
 40)
 41
 42// Dispenser is a type that dispenses tokens, similarly to a lexer,
 43// except that it can do so with some notion of structure and has
 44// some really convenient methods.
 45type Dispenser struct {
 46	filename string
 47	tokens   []Token
 48	cursor   int
 49	nesting  int
 50}
 51
 52// NewDispenser returns a Dispenser, ready to use for parsing the given input.
 53func NewDispenser(filename string, input io.Reader) Dispenser {
 54	tokens, _ := allTokens(input) // ignoring error because nothing to do with it
 55	return Dispenser{
 56		filename: filename,
 57		tokens:   tokens,
 58		cursor:   -1,
 59	}
 60}
 61
 62// NewDispenserTokens returns a Dispenser filled with the given tokens.
 63func NewDispenserTokens(filename string, tokens []Token) Dispenser {
 64	return Dispenser{
 65		filename: filename,
 66		tokens:   tokens,
 67		cursor:   -1,
 68	}
 69}
 70
 71// Next loads the next token. Returns true if a token
 72// was loaded; false otherwise. If false, all tokens
 73// have been consumed.
 74func (d *Dispenser) Next() bool {
 75	if d.cursor < len(d.tokens)-1 {
 76		d.cursor++
 77		return true
 78	}
 79	return false
 80}
 81
 82// NextArg loads the next token if it is on the same
 83// line. Returns true if a token was loaded; false
 84// otherwise. If false, all tokens on the line have
 85// been consumed. It handles imported tokens correctly.
 86func (d *Dispenser) NextArg() bool {
 87	if d.cursor < 0 {
 88		d.cursor++
 89		return true
 90	}
 91	if d.cursor >= len(d.tokens) {
 92		return false
 93	}
 94	if d.cursor < len(d.tokens)-1 &&
 95		d.tokens[d.cursor].File == d.tokens[d.cursor+1].File &&
 96		d.tokens[d.cursor].Line+d.numLineBreaks(d.cursor) == d.tokens[d.cursor+1].Line {
 97		d.cursor++
 98		return true
 99	}
100	return false
101}
102
103// NextLine loads the next token only if it is not on the same
104// line as the current token, and returns true if a token was
105// loaded; false otherwise. If false, there is not another token
106// or it is on the same line. It handles imported tokens correctly.
107func (d *Dispenser) NextLine() bool {
108	if d.cursor < 0 {
109		d.cursor++
110		return true
111	}
112	if d.cursor >= len(d.tokens) {
113		return false
114	}
115	if d.cursor < len(d.tokens)-1 &&
116		(d.tokens[d.cursor].File != d.tokens[d.cursor+1].File ||
117			d.tokens[d.cursor].Line+d.numLineBreaks(d.cursor) < d.tokens[d.cursor+1].Line) {
118		d.cursor++
119		return true
120	}
121	return false
122}
123
124// NextBlock can be used as the condition of a for loop
125// to load the next token as long as it opens a block or
126// is already in a block. It returns true if a token was
127// loaded, or false when the block's closing curly brace
128// was loaded and thus the block ended. Nested blocks are
129// not supported.
130func (d *Dispenser) NextBlock() bool {
131	if d.nesting > 0 {
132		d.Next()
133		if d.Val() == "}" {
134			d.nesting--
135			return false
136		}
137		return true
138	}
139	if !d.NextArg() { // block must open on same line
140		return false
141	}
142	if d.Val() != "{" {
143		d.cursor-- // roll back if not opening brace
144		return false
145	}
146	d.Next()
147	if d.Val() == "}" {
148		// Open and then closed right away
149		return false
150	}
151	d.nesting++
152	return true
153}
154
155// Val gets the text of the current token. If there is no token
156// loaded, it returns empty string.
157func (d *Dispenser) Val() string {
158	if d.cursor < 0 || d.cursor >= len(d.tokens) {
159		return ""
160	}
161	return d.tokens[d.cursor].Text
162}
163
164// Line gets the line number of the current token. If there is no token
165// loaded, it returns 0.
166func (d *Dispenser) Line() int {
167	if d.cursor < 0 || d.cursor >= len(d.tokens) {
168		return 0
169	}
170	return d.tokens[d.cursor].Line
171}
172
173// File gets the filename of the current token. If there is no token loaded,
174// it returns the filename originally given when parsing started.
175func (d *Dispenser) File() string {
176	if d.cursor < 0 || d.cursor >= len(d.tokens) {
177		return d.filename
178	}
179	if tokenFilename := d.tokens[d.cursor].File; tokenFilename != "" {
180		return tokenFilename
181	}
182	return d.filename
183}
184
185// Args is a convenience function that loads the next arguments
186// (tokens on the same line) into an arbitrary number of strings
187// pointed to in targets. If there are fewer tokens available
188// than string pointers, the remaining strings will not be changed
189// and false will be returned. If there were enough tokens available
190// to fill the arguments, then true will be returned.
191func (d *Dispenser) Args(targets ...*string) bool {
192	enough := true
193	for i := 0; i < len(targets); i++ {
194		if !d.NextArg() {
195			enough = false
196			break
197		}
198		*targets[i] = d.Val()
199	}
200	return enough
201}
202
203// RemainingArgs loads any more arguments (tokens on the same line)
204// into a slice and returns them. Open curly brace tokens also indicate
205// the end of arguments, and the curly brace is not included in
206// the return value nor is it loaded.
207func (d *Dispenser) RemainingArgs() []string {
208	var args []string
209
210	for d.NextArg() {
211		if d.Val() == "{" {
212			d.cursor--
213			break
214		}
215		args = append(args, d.Val())
216	}
217
218	return args
219}
220
221// ArgErr returns an argument error, meaning that another
222// argument was expected but not found. In other words,
223// a line break or open curly brace was encountered instead of
224// an argument.
225func (d *Dispenser) ArgErr() error {
226	if d.Val() == "{" {
227		return d.Err("Unexpected token '{', expecting argument")
228	}
229	return d.Errf("Wrong argument count or unexpected line ending after '%s'", d.Val())
230}
231
232// SyntaxErr creates a generic syntax error which explains what was
233// found and what was expected.
234func (d *Dispenser) SyntaxErr(expected string) error {
235	msg := fmt.Sprintf("%s:%d - Syntax error: Unexpected token '%s', expecting '%s'", d.File(), d.Line(), d.Val(), expected)
236	return errors.New(msg)
237}
238
239// EOFErr returns an error indicating that the dispenser reached
240// the end of the input when searching for the next token.
241func (d *Dispenser) EOFErr() error {
242	return d.Errf("Unexpected EOF")
243}
244
245// Err generates a custom parse-time error with a message of msg.
246func (d *Dispenser) Err(msg string) error {
247	msg = fmt.Sprintf("%s:%d - Error during parsing: %s", d.File(), d.Line(), msg)
248	return errors.New(msg)
249}
250
251// Errf is like Err, but for formatted error messages
252func (d *Dispenser) Errf(format string, args ...interface{}) error {
253	return d.Err(fmt.Sprintf(format, args...))
254}
255
256// numLineBreaks counts how many line breaks are in the token
257// value given by the token index tknIdx. It returns 0 if the
258// token does not exist or there are no line breaks.
259func (d *Dispenser) numLineBreaks(tknIdx int) int {
260	if tknIdx < 0 || tknIdx >= len(d.tokens) {
261		return 0
262	}
263	return strings.Count(d.tokens[tknIdx].Text, "\n")
264}