lang: Split lang package out into many subpackages

This is a giant refactor to split the giant lang package into many
subpackages. The most difficult piece was figuring out how to extract
the extra ast structs into their own package, because they needed to
call two functions which also needed to import the ast.

The solution was to separate out those functions into their own
packages, and to pass them into the ast at the root when they're needed,
and to let the relevant ast portions call a handle.

This isn't terribly ugly because we already had a giant data struct
woven through the ast.

The bad part is rebasing any WIP work on top of this.
This commit is contained in:
James Shubin
2021-10-21 03:35:31 -04:00
parent 8ae47bd490
commit 23b5a4729f
23 changed files with 1212 additions and 1129 deletions

View File

@@ -19,6 +19,7 @@ package interfaces
import (
"fmt"
"io"
"sort"
"github.com/purpleidea/mgmt/engine"
@@ -186,6 +187,16 @@ type Data struct {
// deploys, however that is not blocked at the level of this interface.
Downloader Downloader
// LexParser is a function that needs to get passed in to run the lexer
// and parser to build the initial AST. This is passed in this way to
// avoid dependency cycles.
LexParser func(io.Reader) (Stmt, error)
// StrInterpolater is a function that needs to get passed in to run the
// string interpolation. This is passed in this way to avoid dependency
// cycles.
StrInterpolater func(string, *Pos, *Data) (Expr, error)
//World engine.World // TODO: do we need this?
// Prefix provides a unique path prefix that we can namespace in. It is

27
lang/interfaces/parser.go Normal file
View File

@@ -0,0 +1,27 @@
// Mgmt
// Copyright (C) 2013-2021+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package interfaces
// Pos represents a position in the code. This is used by the parser and string
// interpolation.
// TODO: consider expanding with range characteristics.
type Pos struct {
Line int // line number starting at 1
Column int // column number starting at 1
Filename string // optional source filename, if known
}