2015-01-23 23:15:10 +01:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-01-09 04:35:10 +01:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/scanner"
|
2015-03-19 22:49:38 +01:00
|
|
|
"unicode"
|
2015-01-09 04:35:10 +01:00
|
|
|
)
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
var noPos scanner.Position
|
2015-01-09 04:35:10 +01:00
|
|
|
|
|
|
|
type printer struct {
|
|
|
|
defs []Definition
|
2016-06-11 02:27:12 +02:00
|
|
|
comments []*CommentGroup
|
2015-01-09 04:35:10 +01:00
|
|
|
|
|
|
|
curComment int
|
2015-03-19 22:49:38 +01:00
|
|
|
|
|
|
|
pos scanner.Position
|
|
|
|
|
|
|
|
pendingSpace bool
|
|
|
|
pendingNewline int
|
2015-01-09 04:35:10 +01:00
|
|
|
|
|
|
|
output []byte
|
|
|
|
|
2015-01-31 01:38:08 +01:00
|
|
|
indentList []int
|
|
|
|
wsBuf []byte
|
|
|
|
|
2016-07-13 01:03:08 +02:00
|
|
|
skippedComments []*CommentGroup
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newPrinter(file *File) *printer {
|
|
|
|
return &printer{
|
|
|
|
defs: file.Defs,
|
|
|
|
comments: file.Comments,
|
|
|
|
indentList: []int{0},
|
2015-03-19 22:49:38 +01:00
|
|
|
|
|
|
|
// pendingNewLine is initialized to -1 to eat initial spaces if the first token is a comment
|
|
|
|
pendingNewline: -1,
|
|
|
|
|
|
|
|
pos: scanner.Position{
|
|
|
|
Line: 1,
|
2015-01-31 01:38:08 +01:00
|
|
|
},
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Print(file *File) ([]byte, error) {
|
|
|
|
p := newPrinter(file)
|
|
|
|
|
|
|
|
for _, def := range p.defs {
|
|
|
|
p.printDef(def)
|
|
|
|
}
|
|
|
|
p.flush()
|
|
|
|
return p.output, nil
|
|
|
|
}
|
|
|
|
|
2017-06-28 23:35:14 +02:00
|
|
|
func PrintExpression(expression Expression) ([]byte, error) {
|
|
|
|
dummyFile := &File{}
|
|
|
|
p := newPrinter(dummyFile)
|
|
|
|
p.printExpression(expression)
|
|
|
|
p.flush()
|
|
|
|
return p.output, nil
|
|
|
|
}
|
|
|
|
|
2015-01-09 04:35:10 +01:00
|
|
|
func (p *printer) Print() ([]byte, error) {
|
|
|
|
for _, def := range p.defs {
|
|
|
|
p.printDef(def)
|
|
|
|
}
|
|
|
|
p.flush()
|
|
|
|
return p.output, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *printer) printDef(def Definition) {
|
|
|
|
if assignment, ok := def.(*Assignment); ok {
|
|
|
|
p.printAssignment(assignment)
|
|
|
|
} else if module, ok := def.(*Module); ok {
|
|
|
|
p.printModule(module)
|
|
|
|
} else {
|
|
|
|
panic("Unknown definition")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *printer) printAssignment(assignment *Assignment) {
|
2016-06-10 00:52:30 +02:00
|
|
|
p.printToken(assignment.Name, assignment.NamePos)
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
2016-06-10 02:03:57 +02:00
|
|
|
p.printToken(assignment.Assigner, assignment.EqualsPos)
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
2016-06-07 21:28:16 +02:00
|
|
|
p.printExpression(assignment.OrigValue)
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestNewline()
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *printer) printModule(module *Module) {
|
2016-06-10 00:52:30 +02:00
|
|
|
p.printToken(module.Type, module.TypePos)
|
2016-06-07 21:28:16 +02:00
|
|
|
p.printMap(&module.Map)
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestDoubleNewline()
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
func (p *printer) printExpression(value Expression) {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case *Variable:
|
|
|
|
p.printToken(v.Name, v.NamePos)
|
|
|
|
case *Operator:
|
|
|
|
p.printOperator(v)
|
|
|
|
case *Bool:
|
|
|
|
var s string
|
|
|
|
if v.Value {
|
|
|
|
s = "true"
|
|
|
|
} else {
|
|
|
|
s = "false"
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
p.printToken(s, v.LiteralPos)
|
Support parsing int64 in Blueprint file.
Support int64 number instead of int to be more fixed to bit size so
that the underlying arch won't affect overflow cases. Besides,
refection: func (v Value) Int() int64 always cast to int64 no matter the
input is int, int16, int32. Currently we always treat "-" as negative
sign to bind to next value, and "+" as plus operator to add operands
together.
So we allow:
a = 5 + -4 + 5 or a = -4 + 5
But we don't allow:
a = +5 + 4 + -4 since we don't treat "+" as a positive sign, otherwise,
a = 5 + +5 would exist which looks pretty weird. In the future, we may
want fully support number calculator logic eg, "+"/"-" can be
positive/negative sign or operator, and "(" and ")" will be considered
to group expressions with a higher precedence.
int & uint properties within struct keeps unchanged, which is only
allowed when tagged with 'blueprint:mutated'. We only allow *int64
property instead of int64 property within struct since it does't make
sense to do prepending or appending to int64.
Change-Id: I565e046dbd268af3538aee148cd7300037e56523
2017-11-01 22:03:28 +01:00
|
|
|
case *Int64:
|
|
|
|
p.printToken(strconv.FormatInt(v.Value, 10), v.LiteralPos)
|
2016-06-07 21:28:16 +02:00
|
|
|
case *String:
|
|
|
|
p.printToken(strconv.Quote(v.Value), v.LiteralPos)
|
|
|
|
case *List:
|
|
|
|
p.printList(v.Values, v.LBracePos, v.RBracePos)
|
|
|
|
case *Map:
|
|
|
|
p.printMap(v)
|
Select statements
Select statements are a new blueprint feature inspired by bazel's select
statements. They are essentially alternative syntax for soong config
variables that require less boilerplate. In addition, they support
making decisions based on a module's variant, which will eliminate
the need for manual property struct manipulation, such as the arch
mutator's arch: and target: properties.
In order to support decisions based on the variant, select statements
cannot be evaluated as soon as they're parsed. Instead, they must be
stored in the property struct unevaluated. This means that individual
properties need to change their type from say, string, to
Configurable[string]. Currently, only configurable strings, bools, and
string slices are supported, but more types can be added later.
The module implementation must call my_property.Evaluate(ctx) in order
to get the final, resolved value of the select statement.
Bug: 323382414
Test: go tests
Change-Id: I62f8721d7f0ac3d1df4a06d7eaa260a5aa7fcba3
2024-02-02 02:44:27 +01:00
|
|
|
case *Select:
|
|
|
|
p.printSelect(v)
|
2016-06-07 21:28:16 +02:00
|
|
|
default:
|
2016-07-16 01:40:37 +02:00
|
|
|
panic(fmt.Errorf("bad property type: %s", value.Type()))
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Select statements
Select statements are a new blueprint feature inspired by bazel's select
statements. They are essentially alternative syntax for soong config
variables that require less boilerplate. In addition, they support
making decisions based on a module's variant, which will eliminate
the need for manual property struct manipulation, such as the arch
mutator's arch: and target: properties.
In order to support decisions based on the variant, select statements
cannot be evaluated as soon as they're parsed. Instead, they must be
stored in the property struct unevaluated. This means that individual
properties need to change their type from say, string, to
Configurable[string]. Currently, only configurable strings, bools, and
string slices are supported, but more types can be added later.
The module implementation must call my_property.Evaluate(ctx) in order
to get the final, resolved value of the select statement.
Bug: 323382414
Test: go tests
Change-Id: I62f8721d7f0ac3d1df4a06d7eaa260a5aa7fcba3
2024-02-02 02:44:27 +01:00
|
|
|
func (p *printer) printSelect(s *Select) {
|
|
|
|
if len(s.Cases) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(s.Cases) == 1 && s.Cases[0].Pattern.Value == "__soong_conditions_default__" {
|
|
|
|
p.printExpression(s.Cases[0].Value)
|
2024-03-20 00:41:14 +01:00
|
|
|
p.pos = s.RBracePos
|
Select statements
Select statements are a new blueprint feature inspired by bazel's select
statements. They are essentially alternative syntax for soong config
variables that require less boilerplate. In addition, they support
making decisions based on a module's variant, which will eliminate
the need for manual property struct manipulation, such as the arch
mutator's arch: and target: properties.
In order to support decisions based on the variant, select statements
cannot be evaluated as soon as they're parsed. Instead, they must be
stored in the property struct unevaluated. This means that individual
properties need to change their type from say, string, to
Configurable[string]. Currently, only configurable strings, bools, and
string slices are supported, but more types can be added later.
The module implementation must call my_property.Evaluate(ctx) in order
to get the final, resolved value of the select statement.
Bug: 323382414
Test: go tests
Change-Id: I62f8721d7f0ac3d1df4a06d7eaa260a5aa7fcba3
2024-02-02 02:44:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
p.requestSpace()
|
|
|
|
p.printToken("select(", s.KeywordPos)
|
|
|
|
switch s.Typ {
|
|
|
|
case SelectTypeSoongConfigVariable:
|
|
|
|
p.printToken("soong_config_variable(", s.Condition.LiteralPos)
|
|
|
|
parts := strings.Split(s.Condition.Value, ":")
|
|
|
|
namespace := parts[0]
|
|
|
|
variable := parts[1]
|
|
|
|
p.printToken(strconv.Quote(namespace), s.Condition.LiteralPos)
|
|
|
|
p.printToken(",", s.Condition.LiteralPos)
|
|
|
|
p.requestSpace()
|
|
|
|
p.printToken(strconv.Quote(variable), s.Condition.LiteralPos)
|
|
|
|
p.printToken(")", s.Condition.LiteralPos)
|
|
|
|
case SelectTypeReleaseVariable:
|
|
|
|
p.printToken("release_variable(", s.Condition.LiteralPos)
|
|
|
|
p.printToken(strconv.Quote(s.Condition.Value), s.Condition.LiteralPos)
|
|
|
|
p.printToken(")", s.Condition.LiteralPos)
|
|
|
|
case SelectTypeProductVariable:
|
|
|
|
p.printToken("product_variable(", s.Condition.LiteralPos)
|
|
|
|
p.printToken(strconv.Quote(s.Condition.Value), s.Condition.LiteralPos)
|
|
|
|
p.printToken(")", s.Condition.LiteralPos)
|
|
|
|
case SelectTypeVariant:
|
|
|
|
p.printToken("variant(", s.Condition.LiteralPos)
|
|
|
|
p.printToken(strconv.Quote(s.Condition.Value), s.Condition.LiteralPos)
|
|
|
|
p.printToken(")", s.Condition.LiteralPos)
|
|
|
|
default:
|
|
|
|
panic("should be unreachable")
|
|
|
|
}
|
|
|
|
p.printToken(", {", s.LBracePos)
|
|
|
|
p.requestNewline()
|
|
|
|
p.indent(p.curIndent() + 4)
|
|
|
|
for _, c := range s.Cases {
|
|
|
|
p.requestNewline()
|
|
|
|
if c.Pattern.Value != "__soong_conditions_default__" {
|
|
|
|
p.printToken(strconv.Quote(c.Pattern.Value), c.Pattern.LiteralPos)
|
|
|
|
} else {
|
|
|
|
p.printToken("_", c.Pattern.LiteralPos)
|
|
|
|
}
|
|
|
|
p.printToken(":", c.ColonPos)
|
|
|
|
p.requestSpace()
|
|
|
|
p.printExpression(c.Value)
|
|
|
|
p.printToken(",", c.Value.Pos())
|
|
|
|
}
|
|
|
|
p.requestNewline()
|
|
|
|
p.unindent(s.RBracePos)
|
|
|
|
p.printToken("})", s.RBracePos)
|
|
|
|
if s.Append != nil {
|
|
|
|
p.requestSpace()
|
|
|
|
p.printToken("+", s.RBracePos)
|
|
|
|
p.requestSpace()
|
|
|
|
p.printExpression(s.Append)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
func (p *printer) printList(list []Expression, pos, endPos scanner.Position) {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
|
|
|
p.printToken("[", pos)
|
2022-02-09 03:10:12 +01:00
|
|
|
if len(list) > 1 || pos.Line != endPos.Line || listHasMap(list) {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestNewline()
|
2015-01-09 04:35:10 +01:00
|
|
|
p.indent(p.curIndent() + 4)
|
|
|
|
for _, value := range list {
|
2016-06-07 21:28:16 +02:00
|
|
|
p.printExpression(value)
|
2015-03-19 22:49:38 +01:00
|
|
|
p.printToken(",", noPos)
|
|
|
|
p.requestNewline()
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
2015-03-19 22:49:38 +01:00
|
|
|
p.unindent(endPos)
|
2015-01-09 04:35:10 +01:00
|
|
|
} else {
|
|
|
|
for _, value := range list {
|
2016-06-07 21:28:16 +02:00
|
|
|
p.printExpression(value)
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-19 22:49:38 +01:00
|
|
|
p.printToken("]", endPos)
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
func (p *printer) printMap(m *Map) {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
2016-06-07 21:28:16 +02:00
|
|
|
p.printToken("{", m.LBracePos)
|
|
|
|
if len(m.Properties) > 0 || m.LBracePos.Line != m.RBracePos.Line {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestNewline()
|
2015-01-09 04:35:10 +01:00
|
|
|
p.indent(p.curIndent() + 4)
|
2016-06-07 21:28:16 +02:00
|
|
|
for _, prop := range m.Properties {
|
2015-03-03 00:32:36 +01:00
|
|
|
p.printProperty(prop)
|
2015-03-19 22:49:38 +01:00
|
|
|
p.printToken(",", noPos)
|
|
|
|
p.requestNewline()
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
p.unindent(m.RBracePos)
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
p.printToken("}", m.RBracePos)
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
func (p *printer) printOperator(operator *Operator) {
|
2018-05-08 01:11:42 +02:00
|
|
|
p.printOperatorInternal(operator, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *printer) printOperatorInternal(operator *Operator, allowIndent bool) {
|
2016-06-07 21:28:16 +02:00
|
|
|
p.printExpression(operator.Args[0])
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
2016-06-07 21:28:16 +02:00
|
|
|
p.printToken(string(operator.Operator), operator.OperatorPos)
|
2018-05-08 01:11:42 +02:00
|
|
|
|
|
|
|
indented := false
|
2016-06-07 21:28:16 +02:00
|
|
|
if operator.Args[0].End().Line == operator.Args[1].Pos().Line {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
|
|
|
} else {
|
2018-05-08 01:11:42 +02:00
|
|
|
if allowIndent {
|
|
|
|
indented = true
|
|
|
|
p.indent(p.curIndent() + 4)
|
|
|
|
}
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestNewline()
|
|
|
|
}
|
2018-05-08 01:11:42 +02:00
|
|
|
|
|
|
|
if op, isOp := operator.Args[1].(*Operator); isOp {
|
|
|
|
p.printOperatorInternal(op, false)
|
|
|
|
} else {
|
|
|
|
p.printExpression(operator.Args[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
if indented {
|
|
|
|
p.unindent(p.pos)
|
|
|
|
}
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
2015-03-03 00:32:36 +01:00
|
|
|
func (p *printer) printProperty(property *Property) {
|
2016-06-10 00:52:30 +02:00
|
|
|
p.printToken(property.Name, property.NamePos)
|
2016-06-10 02:03:57 +02:00
|
|
|
p.printToken(":", property.ColonPos)
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
2016-06-07 21:28:16 +02:00
|
|
|
p.printExpression(property.Value)
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print a single token, including any necessary comments or whitespace between
|
|
|
|
// this token and the previously printed token
|
2015-03-19 22:49:38 +01:00
|
|
|
func (p *printer) printToken(s string, pos scanner.Position) {
|
|
|
|
newline := p.pendingNewline != 0
|
2015-01-31 01:38:08 +01:00
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
if pos == noPos {
|
|
|
|
pos = p.pos
|
2015-01-31 01:38:08 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
if newline {
|
|
|
|
p.printEndOfLineCommentsBefore(pos)
|
|
|
|
p.requestNewlinesForPos(pos)
|
2015-01-31 01:38:08 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
p.printInLineCommentsBefore(pos)
|
2015-01-31 01:38:08 +01:00
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
p.flushSpace()
|
2015-01-31 01:38:08 +01:00
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
p.output = append(p.output, s...)
|
2015-01-31 01:38:08 +01:00
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
p.pos = pos
|
2015-01-31 01:38:08 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
// Print any in-line (single line /* */) comments that appear _before_ pos
|
|
|
|
func (p *printer) printInLineCommentsBefore(pos scanner.Position) {
|
2016-06-11 02:27:12 +02:00
|
|
|
for p.curComment < len(p.comments) && p.comments[p.curComment].Pos().Offset < pos.Offset {
|
2015-03-19 22:49:38 +01:00
|
|
|
c := p.comments[p.curComment]
|
2016-06-11 02:27:12 +02:00
|
|
|
if c.Comments[0].Comment[0][0:2] == "//" || len(c.Comments[0].Comment) > 1 {
|
2016-07-13 01:03:08 +02:00
|
|
|
p.skippedComments = append(p.skippedComments, c)
|
2015-03-19 22:49:38 +01:00
|
|
|
} else {
|
|
|
|
p.printComment(c)
|
|
|
|
p.requestSpace()
|
|
|
|
}
|
|
|
|
p.curComment++
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
// Print any comments, including end of line comments, that appear _before_ the line specified
|
|
|
|
// by pos
|
|
|
|
func (p *printer) printEndOfLineCommentsBefore(pos scanner.Position) {
|
2016-07-13 01:03:08 +02:00
|
|
|
if len(p.skippedComments) > 0 {
|
|
|
|
for _, c := range p.skippedComments {
|
|
|
|
p.printComment(c)
|
2024-03-20 00:41:14 +01:00
|
|
|
p._requestNewline()
|
2016-07-13 01:03:08 +02:00
|
|
|
}
|
2016-06-11 02:27:12 +02:00
|
|
|
p.skippedComments = nil
|
2015-01-31 01:38:08 +01:00
|
|
|
}
|
2016-06-11 02:27:12 +02:00
|
|
|
for p.curComment < len(p.comments) && p.comments[p.curComment].Pos().Line < pos.Line {
|
2015-03-19 22:49:38 +01:00
|
|
|
c := p.comments[p.curComment]
|
|
|
|
p.printComment(c)
|
|
|
|
p._requestNewline()
|
2015-01-09 04:35:10 +01:00
|
|
|
p.curComment++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
// Compare the line numbers of the previous and current positions to determine whether extra
|
|
|
|
// newlines should be inserted. A second newline is allowed anywhere requestNewline() is called.
|
|
|
|
func (p *printer) requestNewlinesForPos(pos scanner.Position) bool {
|
|
|
|
if pos.Line > p.pos.Line {
|
|
|
|
p._requestNewline()
|
|
|
|
if pos.Line > p.pos.Line+1 {
|
|
|
|
p.pendingNewline = 2
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
2015-03-19 22:49:38 +01:00
|
|
|
return true
|
2015-01-31 01:38:08 +01:00
|
|
|
}
|
2015-03-19 22:49:38 +01:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *printer) requestSpace() {
|
|
|
|
p.pendingSpace = true
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
// Ask for a newline to be inserted before the next token, but do not insert any comments. Used
|
|
|
|
// by the comment printers.
|
|
|
|
func (p *printer) _requestNewline() {
|
|
|
|
if p.pendingNewline == 0 {
|
|
|
|
p.pendingNewline = 1
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
2015-03-19 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for a newline to be inserted before the next token. Also inserts any end-of line comments
|
|
|
|
// for the current line
|
|
|
|
func (p *printer) requestNewline() {
|
|
|
|
pos := p.pos
|
|
|
|
pos.Line++
|
|
|
|
p.printEndOfLineCommentsBefore(pos)
|
|
|
|
p._requestNewline()
|
|
|
|
}
|
2015-01-09 04:35:10 +01:00
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
// Ask for two newlines to be inserted before the next token. Also inserts any end-of line comments
|
|
|
|
// for the current line
|
|
|
|
func (p *printer) requestDoubleNewline() {
|
|
|
|
p.requestNewline()
|
|
|
|
p.pendingNewline = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush any pending whitespace, ignoring pending spaces if there is a pending newline
|
|
|
|
func (p *printer) flushSpace() {
|
|
|
|
if p.pendingNewline == 1 {
|
2015-01-09 04:35:10 +01:00
|
|
|
p.output = append(p.output, '\n')
|
2015-03-19 22:49:38 +01:00
|
|
|
p.pad(p.curIndent())
|
|
|
|
} else if p.pendingNewline == 2 {
|
|
|
|
p.output = append(p.output, "\n\n"...)
|
|
|
|
p.pad(p.curIndent())
|
|
|
|
} else if p.pendingSpace == true && p.pendingNewline != -1 {
|
|
|
|
p.output = append(p.output, ' ')
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
p.pendingSpace = false
|
|
|
|
p.pendingNewline = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print a single comment, which may be a multi-line comment
|
2016-06-11 02:27:12 +02:00
|
|
|
func (p *printer) printComment(cg *CommentGroup) {
|
|
|
|
for _, comment := range cg.Comments {
|
|
|
|
if !p.requestNewlinesForPos(comment.Pos()) {
|
|
|
|
p.requestSpace()
|
2015-03-19 22:49:38 +01:00
|
|
|
}
|
2016-06-11 02:27:12 +02:00
|
|
|
for i, line := range comment.Comment {
|
|
|
|
line = strings.TrimRightFunc(line, unicode.IsSpace)
|
|
|
|
p.flushSpace()
|
|
|
|
if i != 0 {
|
|
|
|
lineIndent := strings.IndexFunc(line, func(r rune) bool { return !unicode.IsSpace(r) })
|
|
|
|
lineIndent = max(lineIndent, p.curIndent())
|
|
|
|
p.pad(lineIndent - p.curIndent())
|
|
|
|
}
|
|
|
|
p.output = append(p.output, strings.TrimSpace(line)...)
|
|
|
|
if i < len(comment.Comment)-1 {
|
|
|
|
p._requestNewline()
|
|
|
|
}
|
2015-03-19 22:49:38 +01:00
|
|
|
}
|
2024-03-20 00:41:14 +01:00
|
|
|
if p.pos.Offset < comment.End().Offset {
|
|
|
|
p.pos = comment.End()
|
|
|
|
}
|
2015-03-19 22:49:38 +01:00
|
|
|
}
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print any comments that occur after the last token, and a trailing newline
|
|
|
|
func (p *printer) flush() {
|
2016-07-13 01:03:08 +02:00
|
|
|
for _, c := range p.skippedComments {
|
|
|
|
if !p.requestNewlinesForPos(c.Pos()) {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
|
|
|
}
|
2016-07-13 01:03:08 +02:00
|
|
|
p.printComment(c)
|
2015-01-31 01:38:08 +01:00
|
|
|
}
|
2015-01-09 04:35:10 +01:00
|
|
|
for p.curComment < len(p.comments) {
|
2016-06-11 02:27:12 +02:00
|
|
|
p.printComment(p.comments[p.curComment])
|
2015-01-09 04:35:10 +01:00
|
|
|
p.curComment++
|
|
|
|
}
|
|
|
|
p.output = append(p.output, '\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print whitespace to pad from column l to column max
|
2015-03-19 22:49:38 +01:00
|
|
|
func (p *printer) pad(l int) {
|
2015-01-09 04:35:10 +01:00
|
|
|
if l > len(p.wsBuf) {
|
|
|
|
p.wsBuf = make([]byte, l)
|
|
|
|
for i := range p.wsBuf {
|
|
|
|
p.wsBuf[i] = ' '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.output = append(p.output, p.wsBuf[0:l]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *printer) indent(i int) {
|
|
|
|
p.indentList = append(p.indentList, i)
|
|
|
|
}
|
|
|
|
|
2015-03-19 22:49:38 +01:00
|
|
|
func (p *printer) unindent(pos scanner.Position) {
|
|
|
|
p.printEndOfLineCommentsBefore(pos)
|
2015-01-09 04:35:10 +01:00
|
|
|
p.indentList = p.indentList[0 : len(p.indentList)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *printer) curIndent() int {
|
|
|
|
return p.indentList[len(p.indentList)-1]
|
|
|
|
}
|
2015-01-31 01:38:08 +01:00
|
|
|
|
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
} else {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
2022-02-09 03:10:12 +01:00
|
|
|
|
|
|
|
func listHasMap(list []Expression) bool {
|
|
|
|
for _, value := range list {
|
|
|
|
if _, ok := value.(*Map); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|