Skip to content

Commit

Permalink
all: remove script support
Browse files Browse the repository at this point in the history
Scripts were an experimental feature, this change removes it.
  • Loading branch information
gazerro committed Jul 5, 2022
1 parent 30a9cf2 commit f423f04
Show file tree
Hide file tree
Showing 45 changed files with 104 additions and 1,000 deletions.
73 changes: 23 additions & 50 deletions ast/astutil/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,39 @@ package astutil_test
import (
"bytes"
"fmt"
"strings"

"github.com/open2b/scriggo/ast"
"github.com/open2b/scriggo/ast/astutil"
"github.com/open2b/scriggo/internal/compiler"
)

func ExampleDump() {
cases := map[ast.Format][]string{
ast.FormatHTML: {
"{{ (4 + 5) * value() }}",
"{% var x = 10 %}",
"{% if true %} some text, blah blah {% end %}",
"{% for i in x %} some text, blah blah blah {% end %}",
"{% for i in x %} some very very very very very very very very long text, blah blah blah {% end %}",
`{{ render "ciao.txt" }}`,
"{{5+6}}",
`{% var x = 10 %}`,
`{% y = 10 %}`,
`{% y = (4 + 5) %}`,
},
ast.FormatText: {
"5 + 6",
"map[string]([]interface{})",
},
cases := []string{
"{{ (4 + 5) * value() }}",
"{% var x = 10 %}",
"{% if true %} some text, blah blah {% end %}",
"{% for i in x %} some text, blah blah blah {% end %}",
"{% for i in x %} some very very very very very very very very long text, blah blah blah {% end %}",
`{{ render "ciao.txt" }}`,
"{{5+6}}",
`{% var x = 10 %}`,
`{% y = 10 %}`,
`{% y = (4 + 5) %}`,
}

for _, format := range []ast.Format{ast.FormatHTML, ast.FormatText} {
stringCases := cases[format]
for _, c := range stringCases {
var tree *ast.Tree
var err error
if format == ast.FormatText {
tree, err = compiler.ParseScript(strings.NewReader(c), nil)
} else {
tree, _, err = compiler.ParseTemplateSource([]byte(c), format, false, false, false, false)
}
if err != nil {
panic(err)
}
var buf bytes.Buffer
err = astutil.Dump(&buf, tree)
if err != nil {
panic(err)
}
got := buf.String()

fmt.Print(got)
for _, c := range cases {
tree, _, err := compiler.ParseTemplateSource([]byte(c), ast.FormatHTML, false, false, false)
if err != nil {
panic(err)
}
var buf bytes.Buffer
err = astutil.Dump(&buf, tree)
if err != nil {
panic(err)
}
got := buf.String()

fmt.Print(got)
}

// Output: Tree: "":1:1
Expand Down Expand Up @@ -115,16 +99,5 @@ func ExampleDump() {
// │ │ BinaryOperator (1:11) 4 + 5
// │ │ │ BasicLiteral (1:9) 4
// │ │ │ BasicLiteral (1:13) 5
//
// Tree: "":1:1
// │ BinaryOperator (1:3) 5 + 6
// │ │ BasicLiteral (1:1) 5
// │ │ BasicLiteral (1:5) 6
//
// Tree: "":1:1
// │ MapType (1:1) map[string][]interface{}
// │ │ Identifier (1:5) string
// │ │ SliceType (1:13) []interface{}
// │ │ │ Interface (1:15) interface{}

}
2 changes: 1 addition & 1 deletion ast/astutil/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestWalk(t *testing.T) {
}

for _, c := range stringCases {
tree, _, err := compiler.ParseTemplateSource([]byte(c.input), ast.FormatHTML, false, false, false, false)
tree, _, err := compiler.ParseTemplateSource([]byte(c.input), ast.FormatHTML, false, false, false)
if err != nil {
panic(err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/scriggo/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ A Scriggofile is a file with a specific format used by the scriggo command.
The scriggo command uses the instructions in a Scriggofile to initialize an
interpreter or a Go source file used in an application that embeds Scriggo.
A Scriggofile defines which packages an interpreted program and script can
import, what exported declarations in a package are accessible and so on.
A Scriggofile defines which packages an interpreted program can import, what
exported declarations in a package are accessible and so on.
The format of the Scriggofile is:
Expand All @@ -255,7 +255,7 @@ The instructions are:
IMPORT STANDARD LIBRARY
Makes the packages in the Go standard library (almost all) importable
in a program or script executed by the interpreter.
in a program executed by the interpreter.
To view all packages imported run 'scriggo stdlib'.
Expand Down Expand Up @@ -292,7 +292,7 @@ The instructions are:
As for 'IMPORT <package> AS main' but the exported names in the package
will be imported not capitalized. For example a name 'FooFoo' declared
in the package will be imported in the script as 'fooFoo'.
in the package will be imported in a template file as 'fooFoo'.
SET VARIABLE <name>
Expand Down
17 changes: 3 additions & 14 deletions internal/compiler/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Converter func(src []byte, out io.Writer) error

const (
programMod checkingMod = iota + 1
scriptMod
templateMod
)

Expand All @@ -51,7 +50,7 @@ func typecheck(tree *ast.Tree, importer native.Importer, opts checkerOptions) (m
return compilation.pkgInfos, nil
}

// Prepare the type checking for scripts and templates.
// Prepare the type checking for templates.
var globalScope map[string]scopeName
if opts.globals != nil {
globals := native.Package{
Expand All @@ -61,16 +60,6 @@ func typecheck(tree *ast.Tree, importer native.Importer, opts checkerOptions) (m
globalScope = toTypeCheckerScope(globals, opts.mod, true, 0)
}

// Add the global "exit" to script global scope.
if opts.mod == scriptMod {
exit := scopeName{ti: &typeInfo{Properties: propertyUniverse}}
if globalScope == nil {
globalScope = map[string]scopeName{"exit": exit}
} else if _, ok := globalScope["exit"]; !ok {
globalScope["exit"] = exit
}
}

compilation := newCompilation(globalScope)
tc := newTypechecker(compilation, tree.Path, opts, importer)

Expand Down Expand Up @@ -98,7 +87,7 @@ func typecheck(tree *ast.Tree, importer native.Importer, opts checkerOptions) (m
tc.path = extends.Tree.Path
}

// Type check a template file or a script.
// Type check a template file.
var err error
tree.Nodes, err = tc.checkNodesInNewScopeError(tree, tree.Nodes)
if err != nil {
Expand Down Expand Up @@ -217,7 +206,7 @@ type usingCheck struct {
}

// newTypechecker creates a new type checker. A global scope may be provided
// for scripts and templates.
// for templates.
func newTypechecker(compilation *compilation, path string, opts checkerOptions, importer native.Importer) *typechecker {
tt := types.NewTypes()
tc := typechecker{
Expand Down
4 changes: 2 additions & 2 deletions internal/compiler/checker_expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func (tc *typechecker) checkIdentifier(ident *ast.Identifier, used bool) *typeIn
}
}

// Handle predeclared variables in templates and scripts.
if tc.opts.mod == templateMod || tc.opts.mod == scriptMod {
// Handle predeclared variables in templates.
if tc.opts.mod == templateMod {
// The identifier refers to a native value that is an up value for
// the current function.
if isUpVar && ti.IsNative() {
Expand Down
4 changes: 2 additions & 2 deletions internal/compiler/checker_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func toTypeCheckerScope(pkg native.ImportablePackage, mod checkingMod, global bo
ti.Type = rv.Type()
}
case native.ImportablePackage:
// Import an auto-imported package. This is supported in scripts and templates only.
// Import an auto-imported package. This is supported in templates only.
if mod == programMod {
panic(fmt.Errorf("scriggo: auto-imported packages are supported only for scripts and templates"))
panic(fmt.Errorf("scriggo: auto-imported packages are only supported for templates"))
}
if depth > 0 {
panic(fmt.Errorf("scriggo: cannot have an auto-imported package inside another auto-imported package"))
Expand Down
16 changes: 7 additions & 9 deletions internal/compiler/checker_scopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// 0, 1 are the universe block. 1 is for the format types.
// 2 is the global block, empty for programs.
// 3 is the file/package block.
// 4+ are the scopes in functions. For templates and scripts, 4 is the main block.
// 4+ are the scopes in functions. For templates, 4 is the main block.
//
type scopes struct {
s []scope
Expand All @@ -37,10 +37,10 @@ type scope struct {
// scopeFunc represents a function scope. Only scopes 4 onwards have a function scope.
type scopeFunc struct {
// node is the node of the function.
// It is nil in scopes 0, 1, 2 and 3. It is also nil in scope 4 for scripts.
// It is nil in scopes 0, 1, 2 and 3.
node *ast.Func
// labels are the labels, declared or only used, in the scope of the function.
// It is nil if there are no labels and it is always nil in scopes 0, 1, 2 and 3.
// It is nil if there are no labels, and it is always nil in scopes 0, 1, 2 and 3.
labels map[string]scopeLabel
}

Expand Down Expand Up @@ -246,9 +246,8 @@ func (scopes *scopes) LookupImport(name string) (*ast.Import, bool) {
return n.impor, true
}

// LookupInFunc lookups name in function scopes, including the main block in
// scripts, and returns its type info, its identifier and true. Otherwise it
// returns nil, nil and false.
// LookupInFunc lookups name in function scopes and returns its type info, its
// identifier and true. Otherwise, it returns nil, nil and false.
func (scopes *scopes) LookupInFunc(name string) (*typeInfo, *ast.Identifier, bool) {
n, i := scopes.lookup(name, 4)
return n.ti, n.decl, i != -1
Expand Down Expand Up @@ -358,7 +357,7 @@ func (scopes *scopes) UnusedImport() *ast.Import {
}

// CurrentFunction returns the function of the current scope or nil if there
// is no function. There is no function for the main block of scripts.
// is no function.
func (scopes *scopes) CurrentFunction() *ast.Func {
c := len(scopes.s) - 1
return scopes.s[c].fn.node
Expand All @@ -374,8 +373,7 @@ func (scopes *scopes) Function(name string) *ast.Func {
}

// Functions returns all the functions up to the function of the current
// scope. If there is no function, it returns nil. There is no function for
// the main block of scripts.
// scope. If there is no function, it returns nil.
func (scopes *scopes) Functions() []*ast.Func {
n := 0
for i, sc := range scopes.s {
Expand Down
10 changes: 3 additions & 7 deletions internal/compiler/checker_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ nodesLoop:

case ast.Expression:

// Handle function and macro declarations in scripts and templates.
// Handle function and macro declarations in templates.
if fun, ok := node.(*ast.Func); ok && fun.Ident != nil && tc.opts.mod != programMod {
if fun.Type.Macro && len(fun.Type.Result) == 0 {
tc.makeMacroResultExplicit(fun)
Expand All @@ -900,8 +900,7 @@ nodesLoop:
[]ast.Expression{fun},
)
// Check the new node, informing the type checker that the
// current assignment is a function declaration in a script
// or a macro declaration in a template.
// current assignment is a macro declaration in a template.
newNodes := []ast.Node{varDecl, nodeAssign}

_ = tc.checkNodes(newNodes)
Expand Down Expand Up @@ -943,9 +942,6 @@ nodesLoop:

// checkImport type checks the import declaration.
func (tc *typechecker) checkImport(impor *ast.Import) error {
if tc.opts.mod == scriptMod && impor.Tree != nil {
panic(internalError("native packages only can be imported in script"))
}

// Import a native package.
if impor.Tree == nil {
Expand Down Expand Up @@ -1070,7 +1066,7 @@ func (tc *typechecker) checkImport(impor *ast.Import) error {
case impor.Ident == nil:

// This form of import in templates has been transformed above, so just
// handle programs and scripts here.
// handle programs here.
tc.scopes.Declare(imported.Name, &typeInfo{value: imported, Properties: propertyIsPackage | propertyHasValue}, nil, impor)
return nil

Expand Down
4 changes: 2 additions & 2 deletions internal/compiler/checker_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestCheckerTemplateExpressions(t *testing.T) {
}
options := checkerOptions{mod: templateMod, formatTypes: formatTypes, mdConverter: mdConverter}
for _, expr := range checkerTemplateExprs {
var lex = scanTemplate([]byte("{{ "+expr.src+" }}"), ast.FormatText, false, false, false)
var lex = scanTemplate([]byte("{{ "+expr.src+" }}"), ast.FormatText, false, false)
func() {
defer func() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -241,7 +241,7 @@ var checkerTemplateExprErrors = []struct {
func TestCheckerTemplateExpressionErrors(t *testing.T) {
options := checkerOptions{mod: templateMod, formatTypes: formatTypes}
for _, expr := range checkerTemplateExprErrors {
var lex = scanTemplate([]byte("{{ "+expr.src+" }}"), ast.FormatText, false, false, false)
var lex = scanTemplate([]byte("{{ "+expr.src+" }}"), ast.FormatText, false, false)
func() {
defer func() {
if r := recover(); r != nil {
Expand Down
Loading

0 comments on commit f423f04

Please sign in to comment.