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
|
|
|
|
comments []Comment
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
skippedComments []Comment
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.printToken(assignment.Name.Name, assignment.Name.Pos)
|
|
|
|
p.requestSpace()
|
|
|
|
p.printToken(assignment.Assigner, assignment.Pos)
|
|
|
|
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) {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.printToken(module.Type.Name, module.Type.Pos)
|
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)
|
|
|
|
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)
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("bad property type: %d", value.Type))
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2015-01-09 04:35:10 +01:00
|
|
|
if len(list) > 1 || pos.Line != endPos.Line {
|
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) {
|
|
|
|
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)
|
|
|
|
if operator.Args[0].End().Line == operator.Args[1].Pos().Line {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
|
|
|
} else {
|
|
|
|
p.requestNewline()
|
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
p.printExpression(operator.Args[1])
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
2015-03-03 00:32:36 +01:00
|
|
|
func (p *printer) printProperty(property *Property) {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.printToken(property.Name.Name, property.Name.Pos)
|
|
|
|
p.printToken(":", property.Pos)
|
|
|
|
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-07 21:28:16 +02:00
|
|
|
for p.curComment < len(p.comments) && p.comments[p.curComment].Slash.Offset < pos.Offset {
|
2015-03-19 22:49:38 +01:00
|
|
|
c := p.comments[p.curComment]
|
|
|
|
if c.Comment[0][0:2] == "//" || len(c.Comment) > 1 {
|
|
|
|
p.skippedComments = append(p.skippedComments, c)
|
|
|
|
} else {
|
|
|
|
p.flushSpace()
|
|
|
|
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) {
|
|
|
|
for _, c := range p.skippedComments {
|
2016-06-07 21:28:16 +02:00
|
|
|
if !p.requestNewlinesForPos(c.Slash) {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
2015-01-31 01:38:08 +01:00
|
|
|
}
|
2015-03-19 22:49:38 +01:00
|
|
|
p.printComment(c)
|
|
|
|
p._requestNewline()
|
2015-01-31 01:38:08 +01:00
|
|
|
}
|
2015-03-19 22:49:38 +01:00
|
|
|
p.skippedComments = []Comment{}
|
2016-06-07 21:28:16 +02:00
|
|
|
for p.curComment < len(p.comments) && p.comments[p.curComment].Slash.Line < pos.Line {
|
2015-03-19 22:49:38 +01:00
|
|
|
c := p.comments[p.curComment]
|
2016-06-07 21:28:16 +02:00
|
|
|
if !p.requestNewlinesForPos(c.Slash) {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
2015-01-31 01:38:08 +01:00
|
|
|
}
|
2015-03-19 22:49:38 +01:00
|
|
|
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
|
|
|
|
func (p *printer) printComment(comment Comment) {
|
2016-06-07 21:28:16 +02:00
|
|
|
pos := comment.Slash
|
2015-03-19 22:49:38 +01: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())
|
|
|
|
pos.Line++
|
|
|
|
}
|
|
|
|
p.output = append(p.output, strings.TrimSpace(line)...)
|
|
|
|
if i < len(comment.Comment)-1 {
|
|
|
|
p._requestNewline()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.pos = pos
|
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() {
|
2015-01-31 01:38:08 +01:00
|
|
|
for _, c := range p.skippedComments {
|
2016-06-07 21:28:16 +02:00
|
|
|
if !p.requestNewlinesForPos(c.Slash) {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
|
|
|
}
|
|
|
|
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) {
|
2015-03-19 22:49:38 +01:00
|
|
|
c := p.comments[p.curComment]
|
2016-06-07 21:28:16 +02:00
|
|
|
if !p.requestNewlinesForPos(c.Slash) {
|
2015-03-19 22:49:38 +01:00
|
|
|
p.requestSpace()
|
|
|
|
}
|
|
|
|
p.printComment(c)
|
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
|
|
|
|
}
|
|
|
|
}
|