2019-01-31 01:14:18 -06:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/actions/workflow-parser/model"
|
|
|
|
"github.com/hashicorp/hcl"
|
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
|
|
|
hclparser "github.com/hashicorp/hcl/hcl/parser"
|
|
|
|
"github.com/hashicorp/hcl/hcl/token"
|
|
|
|
"github.com/soniakeys/graph"
|
|
|
|
)
|
|
|
|
|
|
|
|
const minVersion = 0
|
|
|
|
const maxVersion = 0
|
|
|
|
const maxSecrets = 100
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
type Parser struct {
|
|
|
|
version int
|
|
|
|
actions []*model.Action
|
|
|
|
workflows []*model.Workflow
|
|
|
|
errors errorList
|
2019-01-31 01:14:18 -06:00
|
|
|
|
|
|
|
posMap map[interface{}]ast.Node
|
|
|
|
suppressSeverity Severity
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses a .workflow file and return the actions and global variables found within.
|
|
|
|
func Parse(reader io.Reader, options ...OptionFunc) (*model.Configuration, error) {
|
|
|
|
// FIXME - check context for deadline?
|
|
|
|
b, err := ioutil.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root, err := hcl.ParseBytes(b)
|
|
|
|
if err != nil {
|
|
|
|
if pe, ok := err.(*hclparser.PosError); ok {
|
|
|
|
pos := ErrorPos{File: pe.Pos.Filename, Line: pe.Pos.Line, Column: pe.Pos.Column}
|
2019-02-07 00:36:08 -06:00
|
|
|
errors := errorList{newFatal(pos, pe.Err.Error())}
|
|
|
|
return nil, &Error{
|
2019-01-31 18:36:28 -06:00
|
|
|
message: "unable to parse",
|
2019-01-31 01:14:18 -06:00
|
|
|
Errors: errors,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
p := parseAndValidate(root.Node, options...)
|
|
|
|
if len(p.errors) > 0 {
|
|
|
|
return nil, &Error{
|
2019-01-31 01:14:18 -06:00
|
|
|
message: "unable to parse and validate",
|
2019-02-07 00:36:08 -06:00
|
|
|
Errors: p.errors,
|
|
|
|
Actions: p.actions,
|
|
|
|
Workflows: p.workflows,
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.Configuration{
|
2019-02-07 00:36:08 -06:00
|
|
|
Actions: p.actions,
|
|
|
|
Workflows: p.workflows,
|
2019-01-31 01:14:18 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
// parseAndValidate converts a HCL AST into a Parser and validates
|
2019-01-31 01:14:18 -06:00
|
|
|
// high-level structure.
|
|
|
|
// Parameters:
|
|
|
|
// - root - the contents of a .workflow file, as AST
|
|
|
|
// Returns:
|
2019-02-07 00:36:08 -06:00
|
|
|
// - a Parser structure containing actions and workflow definitions
|
|
|
|
func parseAndValidate(root ast.Node, options ...OptionFunc) *Parser {
|
|
|
|
p := &Parser{
|
2019-01-31 01:14:18 -06:00
|
|
|
posMap: make(map[interface{}]ast.Node),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, option := range options {
|
2019-02-07 00:36:08 -06:00
|
|
|
option(p)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
p.parseRoot(root)
|
|
|
|
p.validate()
|
|
|
|
p.errors.sort()
|
2019-01-31 01:14:18 -06:00
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
return p
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) validate() {
|
|
|
|
p.analyzeDependencies()
|
|
|
|
p.checkCircularDependencies()
|
|
|
|
p.checkActions()
|
|
|
|
p.checkFlows()
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func uniqStrings(items []string) []string {
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
ret := make([]string, 0, len(items))
|
|
|
|
for _, item := range items {
|
|
|
|
if !seen[item] {
|
|
|
|
seen[item] = true
|
|
|
|
ret = append(ret, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkCircularDependencies finds loops in the action graph.
|
|
|
|
// It emits a fatal error for each cycle it finds, in the order (top to
|
|
|
|
// bottom, left to right) they appear in the .workflow file.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) checkCircularDependencies() {
|
|
|
|
// make a map from action name to node ID, which is the index in the p.actions array
|
|
|
|
// That is, p.actions[actionmap[X]].Identifier == X
|
2019-01-31 01:14:18 -06:00
|
|
|
actionmap := make(map[string]graph.NI)
|
2019-02-07 00:36:08 -06:00
|
|
|
for i, action := range p.actions {
|
2019-01-31 01:14:18 -06:00
|
|
|
actionmap[action.Identifier] = graph.NI(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make an adjacency list representation of the action dependency graph
|
2019-02-07 00:36:08 -06:00
|
|
|
adjList := make(graph.AdjacencyList, len(p.actions))
|
|
|
|
for i, action := range p.actions {
|
2019-01-31 01:14:18 -06:00
|
|
|
adjList[i] = make([]graph.NI, 0, len(action.Needs))
|
|
|
|
for _, depName := range action.Needs {
|
|
|
|
if depIdx, ok := actionmap[depName]; ok {
|
|
|
|
adjList[i] = append(adjList[i], depIdx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find cycles, and print a fatal error for each one
|
|
|
|
g := graph.Directed{AdjacencyList: adjList}
|
|
|
|
g.Cycles(func(cycle []graph.NI) bool {
|
2019-02-07 00:36:08 -06:00
|
|
|
node := p.posMap[&p.actions[cycle[len(cycle)-1]].Needs]
|
|
|
|
p.addFatal(node, "Circular dependency on `%s'", p.actions[cycle[0]].Identifier)
|
2019-01-31 01:14:18 -06:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkActions returns error if any actions are syntactically correct but
|
|
|
|
// have structural errors
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) checkActions() {
|
2019-01-31 01:14:18 -06:00
|
|
|
secrets := make(map[string]bool)
|
2019-02-07 00:36:08 -06:00
|
|
|
for _, t := range p.actions {
|
2019-01-31 01:14:18 -06:00
|
|
|
// Ensure the Action has a `uses` attribute
|
|
|
|
if t.Uses == nil {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(p.posMap[t], "Action `%s' must have a `uses' attribute", t.Identifier)
|
2019-01-31 01:14:18 -06:00
|
|
|
// continue, checking other actions
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure there aren't too many secrets
|
|
|
|
for _, str := range t.Secrets {
|
|
|
|
if !secrets[str] {
|
|
|
|
secrets[str] = true
|
|
|
|
if len(secrets) == maxSecrets+1 {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(p.posMap[&t.Secrets], "All actions combined must not have more than %d unique secrets", maxSecrets)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that no environment variable or secret begins with
|
|
|
|
// "GITHUB_", unless it's "GITHUB_TOKEN".
|
|
|
|
// Also ensure that all environment variable names come from the legal
|
|
|
|
// form for environment variable names.
|
|
|
|
// Finally, ensure that the same key name isn't used more than once
|
|
|
|
// between env and secrets, combined.
|
|
|
|
for k := range t.Env {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.checkEnvironmentVariable(k, p.posMap[&t.Env])
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
secretVars := make(map[string]bool)
|
|
|
|
for _, k := range t.Secrets {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.checkEnvironmentVariable(k, p.posMap[&t.Secrets])
|
2019-01-31 01:14:18 -06:00
|
|
|
if _, found := t.Env[k]; found {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(p.posMap[&t.Secrets], "Secret `%s' conflicts with an environment variable with the same name", k)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
if secretVars[k] {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addWarning(p.posMap[&t.Secrets], "Secret `%s' redefined", k)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
secretVars[k] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var envVarChecker = regexp.MustCompile(`\A[A-Za-z_][A-Za-z_0-9]*\z`)
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) checkEnvironmentVariable(key string, node ast.Node) {
|
2019-01-31 01:14:18 -06:00
|
|
|
if key != "GITHUB_TOKEN" && strings.HasPrefix(key, "GITHUB_") {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addWarning(node, "Environment variables and secrets beginning with `GITHUB_' are reserved")
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
if !envVarChecker.MatchString(key) {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addWarning(node, "Environment variables and secrets must contain only A-Z, a-z, 0-9, and _ characters, got `%s'", key)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkFlows appends an error if any workflows are syntactically correct but
|
|
|
|
// have structural errors
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) checkFlows() {
|
|
|
|
actionmap := makeActionMap(p.actions)
|
|
|
|
for _, f := range p.workflows {
|
2019-01-31 01:14:18 -06:00
|
|
|
// make sure there's an `on` attribute
|
|
|
|
if f.On == "" {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(p.posMap[f], "Workflow `%s' must have an `on' attribute", f.Identifier)
|
2019-01-31 01:14:18 -06:00
|
|
|
// continue, checking other workflows
|
2019-02-07 00:36:08 -06:00
|
|
|
} else if !isAllowedEventType(f.On) {
|
|
|
|
p.addError(p.posMap[&f.On], "Workflow `%s' has unknown `on' value `%s'", f.Identifier, f.On)
|
2019-01-31 01:14:18 -06:00
|
|
|
// continue, checking other workflows
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that the actions that are resolved all exist
|
|
|
|
for _, actionID := range f.Resolves {
|
|
|
|
_, ok := actionmap[actionID]
|
|
|
|
if !ok {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(p.posMap[&f.Resolves], "Workflow `%s' resolves unknown action `%s'", f.Identifier, actionID)
|
2019-01-31 01:14:18 -06:00
|
|
|
// continue, checking other workflows
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeActionMap(actions []*model.Action) map[string]*model.Action {
|
|
|
|
actionmap := make(map[string]*model.Action)
|
|
|
|
for _, action := range actions {
|
|
|
|
actionmap[action.Identifier] = action
|
|
|
|
}
|
|
|
|
return actionmap
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in Action dependencies for all actions based on explicit dependencies
|
|
|
|
// declarations.
|
|
|
|
//
|
2019-02-07 00:36:08 -06:00
|
|
|
// p.actions is an array of Action objects, as parsed. The Action objects in
|
2019-01-31 01:14:18 -06:00
|
|
|
// this array are mutated, by setting Action.dependencies for each.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) analyzeDependencies() {
|
|
|
|
actionmap := makeActionMap(p.actions)
|
|
|
|
for _, action := range p.actions {
|
2019-01-31 01:14:18 -06:00
|
|
|
// analyze explicit dependencies for each "needs" keyword
|
2019-02-07 00:36:08 -06:00
|
|
|
p.analyzeNeeds(action, actionmap)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// uniq all the dependencies lists
|
2019-02-07 00:36:08 -06:00
|
|
|
for _, action := range p.actions {
|
2019-01-31 01:14:18 -06:00
|
|
|
if len(action.Needs) >= 2 {
|
|
|
|
action.Needs = uniqStrings(action.Needs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) analyzeNeeds(action *model.Action, actionmap map[string]*model.Action) {
|
2019-01-31 01:14:18 -06:00
|
|
|
for _, need := range action.Needs {
|
|
|
|
_, ok := actionmap[need]
|
|
|
|
if !ok {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(p.posMap[&action.Needs], "Action `%s' needs nonexistent action `%s'", action.Identifier, need)
|
2019-01-31 01:14:18 -06:00
|
|
|
// continue, checking other actions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// literalToStringMap converts a object value from the AST to a
|
|
|
|
// map[string]string. For example, the HCL `{ a="b" c="d" }` becomes the
|
|
|
|
// Go expression map[string]string{ "a": "b", "c": "d" }.
|
|
|
|
// If the value doesn't adhere to that format -- e.g.,
|
|
|
|
// if it's not an object, or it has non-assignment attributes, or if any
|
|
|
|
// of its values are anything other than a string, the function appends an
|
|
|
|
// appropriate error.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) literalToStringMap(node ast.Node) map[string]string {
|
2019-01-31 01:14:18 -06:00
|
|
|
obj, ok := node.(*ast.ObjectType)
|
|
|
|
|
|
|
|
if !ok {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(node, "Expected object, got %s", typename(node))
|
2019-01-31 01:14:18 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
p.checkAssignmentsOnly(obj.List, "")
|
2019-01-31 01:14:18 -06:00
|
|
|
|
|
|
|
ret := make(map[string]string)
|
|
|
|
for _, item := range obj.List.Items {
|
|
|
|
if !isAssignment(item) {
|
|
|
|
continue
|
|
|
|
}
|
2019-02-07 00:36:08 -06:00
|
|
|
str, ok := p.literalToString(item.Val)
|
2019-01-31 01:14:18 -06:00
|
|
|
if ok {
|
2019-02-07 00:36:08 -06:00
|
|
|
key := p.identString(item.Keys[0].Token)
|
2019-01-31 01:14:18 -06:00
|
|
|
if key != "" {
|
|
|
|
if _, found := ret[key]; found {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addWarning(node, "Environment variable `%s' redefined", key)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
ret[key] = str
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) identString(t token.Token) string {
|
2019-01-31 01:14:18 -06:00
|
|
|
switch t.Type {
|
|
|
|
case token.STRING:
|
|
|
|
return t.Value().(string)
|
|
|
|
case token.IDENT:
|
|
|
|
return t.Text
|
|
|
|
default:
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addErrorFromToken(t,
|
2019-01-31 01:14:18 -06:00
|
|
|
"Each identifier should be a string, got %s",
|
|
|
|
strings.ToLower(t.Type.String()))
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// literalToStringArray converts a list value from the AST to a []string.
|
|
|
|
// For example, the HCL `[ "a", "b", "c" ]` becomes the Go expression
|
|
|
|
// []string{ "a", "b", "c" }.
|
|
|
|
// If the value doesn't adhere to that format -- it's not a list, or it
|
|
|
|
// contains anything other than strings, the function appends an
|
|
|
|
// appropriate error.
|
|
|
|
// If promoteScalars is true, then values that are scalar strings are
|
|
|
|
// promoted to a single-entry string array. E.g., "foo" becomes the Go
|
|
|
|
// expression []string{ "foo" }.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) literalToStringArray(node ast.Node, promoteScalars bool) ([]string, bool) {
|
2019-01-31 01:14:18 -06:00
|
|
|
literal, ok := node.(*ast.LiteralType)
|
|
|
|
if ok {
|
|
|
|
if promoteScalars && literal.Token.Type == token.STRING {
|
|
|
|
return []string{literal.Token.Value().(string)}, true
|
|
|
|
}
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(node, "Expected list, got %s", typename(node))
|
2019-01-31 01:14:18 -06:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
list, ok := node.(*ast.ListType)
|
|
|
|
if !ok {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(node, "Expected list, got %s", typename(node))
|
2019-01-31 01:14:18 -06:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := make([]string, 0, len(list.List))
|
|
|
|
for _, literal := range list.List {
|
2019-02-07 00:36:08 -06:00
|
|
|
str, ok := p.literalToString(literal)
|
2019-01-31 01:14:18 -06:00
|
|
|
if ok {
|
|
|
|
ret = append(ret, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// literalToString converts a literal value from the AST into a string.
|
|
|
|
// If the value isn't a scalar or isn't a string, the function appends an
|
|
|
|
// appropriate error and returns "", false.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) literalToString(node ast.Node) (string, bool) {
|
|
|
|
val := p.literalCast(node, token.STRING)
|
2019-01-31 01:14:18 -06:00
|
|
|
if val == nil {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
return val.(string), true
|
|
|
|
}
|
|
|
|
|
|
|
|
// literalToInt converts a literal value from the AST into an int64.
|
|
|
|
// Supported number formats are: 123, 0x123, and 0123.
|
|
|
|
// Exponents (1e6) and floats (123.456) generate errors.
|
|
|
|
// If the value isn't a scalar or isn't a number, the function appends an
|
|
|
|
// appropriate error and returns 0, false.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) literalToInt(node ast.Node) (int64, bool) {
|
|
|
|
val := p.literalCast(node, token.NUMBER)
|
2019-01-31 01:14:18 -06:00
|
|
|
if val == nil {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return val.(int64), true
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) literalCast(node ast.Node, t token.Type) interface{} {
|
2019-01-31 01:14:18 -06:00
|
|
|
literal, ok := node.(*ast.LiteralType)
|
|
|
|
if !ok {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(node, "Expected %s, got %s", strings.ToLower(t.String()), typename(node))
|
2019-01-31 01:14:18 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if literal.Token.Type != t {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(node, "Expected %s, got %s", strings.ToLower(t.String()), typename(node))
|
2019-01-31 01:14:18 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return literal.Token.Value()
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
// parseRoot parses the root of the AST, filling in p.version, p.actions,
|
|
|
|
// and p.workflows.
|
|
|
|
func (p *Parser) parseRoot(node ast.Node) {
|
2019-01-31 01:14:18 -06:00
|
|
|
objectList, ok := node.(*ast.ObjectList)
|
|
|
|
if !ok {
|
|
|
|
// It should be impossible for HCL to return anything other than an
|
|
|
|
// ObjectList as the root node. This error should never happen.
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(node, "Internal error: root node must be an ObjectList")
|
2019-01-31 01:14:18 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
p.actions = make([]*model.Action, 0, len(objectList.Items))
|
|
|
|
p.workflows = make([]*model.Workflow, 0, len(objectList.Items))
|
2019-01-31 01:14:18 -06:00
|
|
|
identifiers := make(map[string]bool)
|
|
|
|
for idx, item := range objectList.Items {
|
|
|
|
if item.Assign.IsValid() {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.parseVersion(idx, item)
|
2019-01-31 01:14:18 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-02-07 00:36:08 -06:00
|
|
|
p.parseBlock(item, identifiers)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseBlock parses a single, top-level "action" or "workflow" block,
|
2019-02-07 00:36:08 -06:00
|
|
|
// appending it to p.actions or p.workflows as appropriate.
|
|
|
|
func (p *Parser) parseBlock(item *ast.ObjectItem, identifiers map[string]bool) {
|
2019-01-31 01:14:18 -06:00
|
|
|
if len(item.Keys) != 2 {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(item, "Invalid toplevel declaration")
|
2019-01-31 01:14:18 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
cmd := p.identString(item.Keys[0].Token)
|
2019-01-31 01:14:18 -06:00
|
|
|
var id string
|
|
|
|
|
|
|
|
switch cmd {
|
|
|
|
case "action":
|
2019-02-07 00:36:08 -06:00
|
|
|
action := p.actionifyItem(item)
|
2019-01-31 01:14:18 -06:00
|
|
|
if action != nil {
|
|
|
|
id = action.Identifier
|
2019-02-07 00:36:08 -06:00
|
|
|
p.actions = append(p.actions, action)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
case "workflow":
|
2019-02-07 00:36:08 -06:00
|
|
|
workflow := p.workflowifyItem(item)
|
2019-01-31 01:14:18 -06:00
|
|
|
if workflow != nil {
|
|
|
|
id = workflow.Identifier
|
2019-02-07 00:36:08 -06:00
|
|
|
p.workflows = append(p.workflows, workflow)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
default:
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(item, "Invalid toplevel keyword, `%s'", cmd)
|
2019-01-31 01:14:18 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if identifiers[id] {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(item, "Identifier `%s' redefined", id)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
identifiers[id] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseVersion parses a top-level `version=N` statement, filling in
|
2019-02-07 00:36:08 -06:00
|
|
|
// p.version.
|
|
|
|
func (p *Parser) parseVersion(idx int, item *ast.ObjectItem) {
|
|
|
|
if len(item.Keys) != 1 || p.identString(item.Keys[0].Token) != "version" {
|
2019-01-31 01:14:18 -06:00
|
|
|
// not a valid `version` declaration
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(item.Val, "Toplevel declarations cannot be assignments")
|
2019-01-31 01:14:18 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if idx != 0 {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(item.Val, "`version` must be the first declaration")
|
2019-01-31 01:14:18 -06:00
|
|
|
return
|
|
|
|
}
|
2019-02-07 00:36:08 -06:00
|
|
|
version, ok := p.literalToInt(item.Val)
|
2019-01-31 01:14:18 -06:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if version < minVersion || version > maxVersion {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(item.Val, "`version = %d` is not supported", version)
|
2019-01-31 01:14:18 -06:00
|
|
|
return
|
|
|
|
}
|
2019-02-07 00:36:08 -06:00
|
|
|
p.version = int(version)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseIdentifier parses the double-quoted identifier (name) for a
|
|
|
|
// "workflow" or "action" block.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) parseIdentifier(key *ast.ObjectKey) string {
|
2019-01-31 01:14:18 -06:00
|
|
|
id := key.Token.Text
|
|
|
|
if len(id) < 3 || id[0] != '"' || id[len(id)-1] != '"' {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(key, "Invalid format for identifier `%s'", id)
|
2019-01-31 01:14:18 -06:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return id[1 : len(id)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseRequiredString parses a string value, setting its value into the
|
|
|
|
// out-parameter `value` and returning true if successful.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) parseRequiredString(value *string, val ast.Node, nodeType, name, id string) bool {
|
2019-01-31 01:14:18 -06:00
|
|
|
if *value != "" {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addWarning(val, "`%s' redefined in %s `%s'", name, nodeType, id)
|
2019-01-31 01:14:18 -06:00
|
|
|
// continue, allowing the redefinition
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
newVal, ok := p.literalToString(val)
|
2019-01-31 01:14:18 -06:00
|
|
|
if !ok {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(val, "Invalid format for `%s' in %s `%s', expected string", name, nodeType, id)
|
2019-01-31 01:14:18 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if newVal == "" {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(val, "`%s' value in %s `%s' cannot be blank", name, nodeType, id)
|
2019-01-31 01:14:18 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = newVal
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseBlockPreamble parses the beginning of a "workflow" or "action"
|
|
|
|
// block.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) parseBlockPreamble(item *ast.ObjectItem, nodeType string) (string, *ast.ObjectType) {
|
|
|
|
id := p.parseIdentifier(item.Keys[1])
|
2019-01-31 01:14:18 -06:00
|
|
|
if id == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
node := item.Val
|
|
|
|
obj, ok := node.(*ast.ObjectType)
|
|
|
|
if !ok {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(node, "Each %s must have an { ... } block", nodeType)
|
2019-01-31 01:14:18 -06:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
p.checkAssignmentsOnly(obj.List, id)
|
2019-01-31 01:14:18 -06:00
|
|
|
|
|
|
|
return id, obj
|
|
|
|
}
|
|
|
|
|
|
|
|
// actionifyItem converts an AST block to an Action object.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) actionifyItem(item *ast.ObjectItem) *model.Action {
|
|
|
|
id, obj := p.parseBlockPreamble(item, "action")
|
2019-01-31 01:14:18 -06:00
|
|
|
if obj == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
action := &model.Action{
|
|
|
|
Identifier: id,
|
|
|
|
}
|
2019-02-07 00:36:08 -06:00
|
|
|
p.posMap[action] = item
|
2019-01-31 01:14:18 -06:00
|
|
|
|
|
|
|
for _, item := range obj.List.Items {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.parseActionAttribute(p.identString(item.Keys[0].Token), action, item.Val)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseActionAttribute parses a single key-value pair from an "action"
|
|
|
|
// block. This function rejects any unknown keys and enforces formatting
|
|
|
|
// requirements on all values.
|
|
|
|
// It also has higher-than-normal cyclomatic complexity, so we ask the
|
|
|
|
// gocyclo linter to ignore it.
|
|
|
|
// nolint: gocyclo
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) parseActionAttribute(name string, action *model.Action, val ast.Node) {
|
2019-01-31 01:14:18 -06:00
|
|
|
switch name {
|
|
|
|
case "uses":
|
2019-02-07 00:36:08 -06:00
|
|
|
p.parseUses(action, val)
|
2019-01-31 01:14:18 -06:00
|
|
|
case "needs":
|
2019-02-07 00:36:08 -06:00
|
|
|
if needs, ok := p.literalToStringArray(val, true); ok {
|
2019-01-31 01:14:18 -06:00
|
|
|
action.Needs = needs
|
2019-02-07 00:36:08 -06:00
|
|
|
p.posMap[&action.Needs] = val
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
case "runs":
|
2019-02-07 00:36:08 -06:00
|
|
|
if runs := p.parseCommand(action, action.Runs, name, val, false); runs != nil {
|
2019-01-31 18:36:28 -06:00
|
|
|
action.Runs = runs
|
|
|
|
}
|
2019-01-31 01:14:18 -06:00
|
|
|
case "args":
|
2019-02-07 00:36:08 -06:00
|
|
|
if args := p.parseCommand(action, action.Args, name, val, true); args != nil {
|
2019-01-31 18:36:28 -06:00
|
|
|
action.Args = args
|
|
|
|
}
|
2019-01-31 01:14:18 -06:00
|
|
|
case "env":
|
2019-02-07 00:36:08 -06:00
|
|
|
if env := p.literalToStringMap(val); env != nil {
|
2019-01-31 01:14:18 -06:00
|
|
|
action.Env = env
|
|
|
|
}
|
2019-02-07 00:36:08 -06:00
|
|
|
p.posMap[&action.Env] = val
|
2019-01-31 01:14:18 -06:00
|
|
|
case "secrets":
|
2019-02-07 00:36:08 -06:00
|
|
|
if secrets, ok := p.literalToStringArray(val, false); ok {
|
2019-01-31 01:14:18 -06:00
|
|
|
action.Secrets = secrets
|
2019-02-07 00:36:08 -06:00
|
|
|
p.posMap[&action.Secrets] = val
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
default:
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addWarning(val, "Unknown action attribute `%s'", name)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseUses sets the action.Uses value based on the contents of the AST
|
|
|
|
// node. This function enforces formatting requirements on the value.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) parseUses(action *model.Action, node ast.Node) {
|
2019-01-31 01:14:18 -06:00
|
|
|
if action.Uses != nil {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addWarning(node, "`uses' redefined in action `%s'", action.Identifier)
|
2019-01-31 01:14:18 -06:00
|
|
|
// continue, allowing the redefinition
|
|
|
|
}
|
2019-02-07 00:36:08 -06:00
|
|
|
strVal, ok := p.literalToString(node)
|
2019-01-31 01:14:18 -06:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strVal == "" {
|
|
|
|
action.Uses = &model.UsesInvalid{}
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(node, "`uses' value in action `%s' cannot be blank", action.Identifier)
|
2019-01-31 01:14:18 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(strVal, "./") {
|
|
|
|
action.Uses = &model.UsesPath{Path: strings.TrimPrefix(strVal, "./")}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(strVal, "docker://") {
|
|
|
|
action.Uses = &model.UsesDockerImage{Image: strings.TrimPrefix(strVal, "docker://")}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tok := strings.Split(strVal, "@")
|
|
|
|
if len(tok) != 2 {
|
|
|
|
action.Uses = &model.UsesInvalid{Raw: strVal}
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(node, "The `uses' attribute must be a path, a Docker image, or owner/repo@ref")
|
2019-01-31 01:14:18 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ref := tok[1]
|
|
|
|
tok = strings.SplitN(tok[0], "/", 3)
|
|
|
|
if len(tok) < 2 {
|
|
|
|
action.Uses = &model.UsesInvalid{Raw: strVal}
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(node, "The `uses' attribute must be a path, a Docker image, or owner/repo@ref")
|
2019-01-31 01:14:18 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
usesRepo := &model.UsesRepository{Repository: tok[0] + "/" + tok[1], Ref: ref}
|
|
|
|
action.Uses = usesRepo
|
|
|
|
if len(tok) == 3 {
|
|
|
|
usesRepo.Path = tok[2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 18:36:28 -06:00
|
|
|
// parseUses sets the action.Runs or action.Args value based on the
|
2019-01-31 01:14:18 -06:00
|
|
|
// contents of the AST node. This function enforces formatting
|
|
|
|
// requirements on the value.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) parseCommand(action *model.Action, cmd model.Command, name string, node ast.Node, allowBlank bool) model.Command {
|
2019-01-31 18:36:28 -06:00
|
|
|
if cmd != nil {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addWarning(node, "`%s' redefined in action `%s'", name, action.Identifier)
|
2019-01-31 01:14:18 -06:00
|
|
|
// continue, allowing the redefinition
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is it a list?
|
|
|
|
if _, ok := node.(*ast.ListType); ok {
|
2019-02-07 00:36:08 -06:00
|
|
|
if parsed, ok := p.literalToStringArray(node, false); ok {
|
2019-01-31 18:36:28 -06:00
|
|
|
return &model.ListCommand{Values: parsed}
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
2019-01-31 18:36:28 -06:00
|
|
|
return nil
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// If not, parse a whitespace-separated string into a list.
|
|
|
|
var raw string
|
|
|
|
var ok bool
|
2019-02-07 00:36:08 -06:00
|
|
|
if raw, ok = p.literalToString(node); !ok {
|
|
|
|
p.addError(node, "The `%s' attribute must be a string or a list", name)
|
2019-01-31 18:36:28 -06:00
|
|
|
return nil
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
if raw == "" && !allowBlank {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(node, "`%s' value in action `%s' cannot be blank", name, action.Identifier)
|
2019-01-31 18:36:28 -06:00
|
|
|
return nil
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
2019-01-31 18:36:28 -06:00
|
|
|
return &model.StringCommand{Value: raw}
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func typename(val interface{}) string {
|
|
|
|
switch cast := val.(type) {
|
|
|
|
case *ast.ListType:
|
|
|
|
return "list"
|
|
|
|
case *ast.LiteralType:
|
|
|
|
return strings.ToLower(cast.Token.Type.String())
|
|
|
|
case *ast.ObjectType:
|
|
|
|
return "object"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%T", val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// workflowifyItem converts an AST block to a Workflow object.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) workflowifyItem(item *ast.ObjectItem) *model.Workflow {
|
|
|
|
id, obj := p.parseBlockPreamble(item, "workflow")
|
2019-01-31 01:14:18 -06:00
|
|
|
if obj == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
workflow := &model.Workflow{Identifier: id}
|
|
|
|
for _, item := range obj.List.Items {
|
2019-02-07 00:36:08 -06:00
|
|
|
name := p.identString(item.Keys[0].Token)
|
2019-01-31 01:14:18 -06:00
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "on":
|
2019-02-07 00:36:08 -06:00
|
|
|
ok = p.parseRequiredString(&workflow.On, item.Val, "workflow", name, id)
|
2019-01-31 01:14:18 -06:00
|
|
|
if ok {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.posMap[&workflow.On] = item
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
case "resolves":
|
|
|
|
if workflow.Resolves != nil {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addWarning(item.Val, "`resolves' redefined in workflow `%s'", id)
|
2019-01-31 01:14:18 -06:00
|
|
|
// continue, allowing the redefinition
|
|
|
|
}
|
2019-02-07 00:36:08 -06:00
|
|
|
workflow.Resolves, ok = p.literalToStringArray(item.Val, true)
|
|
|
|
p.posMap[&workflow.Resolves] = item
|
2019-01-31 01:14:18 -06:00
|
|
|
if !ok {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addError(item.Val, "Invalid format for `resolves' in workflow `%s', expected list of strings", id)
|
2019-01-31 01:14:18 -06:00
|
|
|
// continue, allowing workflow with no `resolves`
|
|
|
|
}
|
|
|
|
default:
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addWarning(item.Val, "Unknown workflow attribute `%s'", name)
|
2019-01-31 01:14:18 -06:00
|
|
|
// continue, treat as no-op
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
p.posMap[workflow] = item
|
2019-01-31 01:14:18 -06:00
|
|
|
return workflow
|
|
|
|
}
|
|
|
|
|
|
|
|
func isAssignment(item *ast.ObjectItem) bool {
|
|
|
|
return len(item.Keys) == 1 && item.Assign.IsValid()
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkAssignmentsOnly ensures that all elements in the object are "key =
|
|
|
|
// value" pairs.
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) checkAssignmentsOnly(objectList *ast.ObjectList, actionID string) {
|
2019-01-31 01:14:18 -06:00
|
|
|
for _, item := range objectList.Items {
|
|
|
|
if !isAssignment(item) {
|
|
|
|
var desc string
|
|
|
|
if actionID == "" {
|
|
|
|
desc = "the object"
|
|
|
|
} else {
|
|
|
|
desc = fmt.Sprintf("action `%s'", actionID)
|
|
|
|
}
|
2019-02-07 00:36:08 -06:00
|
|
|
p.addErrorFromObjectItem(item, "Each attribute of %s must be an assignment", desc)
|
2019-01-31 01:14:18 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
child, ok := item.Val.(*ast.ObjectType)
|
|
|
|
if ok {
|
2019-02-07 00:36:08 -06:00
|
|
|
p.checkAssignmentsOnly(child.List, actionID)
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) addWarning(node ast.Node, format string, a ...interface{}) {
|
|
|
|
if p.suppressSeverity < WARNING {
|
|
|
|
p.errors = append(p.errors, newWarning(posFromNode(node), format, a...))
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) addError(node ast.Node, format string, a ...interface{}) {
|
|
|
|
if p.suppressSeverity < ERROR {
|
|
|
|
p.errors = append(p.errors, newError(posFromNode(node), format, a...))
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) addErrorFromToken(t token.Token, format string, a ...interface{}) {
|
|
|
|
if p.suppressSeverity < ERROR {
|
|
|
|
p.errors = append(p.errors, newError(posFromToken(t), format, a...))
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) addErrorFromObjectItem(objectItem *ast.ObjectItem, format string, a ...interface{}) {
|
|
|
|
if p.suppressSeverity < ERROR {
|
|
|
|
p.errors = append(p.errors, newError(posFromObjectItem(objectItem), format, a...))
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:36:08 -06:00
|
|
|
func (p *Parser) addFatal(node ast.Node, format string, a ...interface{}) {
|
|
|
|
if p.suppressSeverity < FATAL {
|
|
|
|
p.errors = append(p.errors, newFatal(posFromNode(node), format, a...))
|
2019-01-31 01:14:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// posFromNode returns an ErrorPos (file, line, and column) from an AST
|
|
|
|
// node, so we can report specific locations for each parse error.
|
|
|
|
func posFromNode(node ast.Node) ErrorPos {
|
|
|
|
var pos *token.Pos
|
|
|
|
switch cast := node.(type) {
|
|
|
|
case *ast.ObjectList:
|
|
|
|
if len(cast.Items) > 0 {
|
|
|
|
if len(cast.Items[0].Keys) > 0 {
|
|
|
|
pos = &cast.Items[0].Keys[0].Token.Pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *ast.ObjectItem:
|
|
|
|
return posFromNode(cast.Val)
|
|
|
|
case *ast.ObjectType:
|
|
|
|
pos = &cast.Lbrace
|
|
|
|
case *ast.LiteralType:
|
|
|
|
pos = &cast.Token.Pos
|
|
|
|
case *ast.ListType:
|
|
|
|
pos = &cast.Lbrack
|
|
|
|
case *ast.ObjectKey:
|
|
|
|
pos = &cast.Token.Pos
|
|
|
|
}
|
|
|
|
|
|
|
|
if pos == nil {
|
|
|
|
return ErrorPos{}
|
|
|
|
}
|
|
|
|
return ErrorPos{File: pos.Filename, Line: pos.Line, Column: pos.Column}
|
|
|
|
}
|
|
|
|
|
|
|
|
// posFromObjectItem returns an ErrorPos from an ObjectItem. This is for
|
|
|
|
// cases where posFromNode(item) would fail because the item has no Val
|
|
|
|
// set.
|
|
|
|
func posFromObjectItem(item *ast.ObjectItem) ErrorPos {
|
|
|
|
if len(item.Keys) > 0 {
|
|
|
|
return posFromNode(item.Keys[0])
|
|
|
|
}
|
|
|
|
return ErrorPos{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// posFromToken returns an ErrorPos from a Token. We can't use
|
|
|
|
// posFromNode here because Tokens aren't Nodes.
|
|
|
|
func posFromToken(token token.Token) ErrorPos {
|
|
|
|
return ErrorPos{File: token.Pos.Filename, Line: token.Pos.Line, Column: token.Pos.Column}
|
|
|
|
}
|