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 (
|
2020-12-14 20:30:48 +01:00
|
|
|
"fmt"
|
2015-01-09 04:35:10 +01:00
|
|
|
"sort"
|
2020-12-14 20:30:48 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-01-09 04:35:10 +01:00
|
|
|
"text/scanner"
|
|
|
|
)
|
|
|
|
|
2021-03-09 02:55:50 +01:00
|
|
|
// numericStringLess compares two strings, returning a lexicographical comparison unless the first
|
|
|
|
// difference occurs in a sequence of 1 or more numeric characters, in which case it returns the
|
|
|
|
// numerical comparison of the two numbers.
|
2020-12-14 20:30:48 +01:00
|
|
|
func numericStringLess(a, b string) bool {
|
2021-03-09 02:55:50 +01:00
|
|
|
isNumeric := func(r rune) bool { return r >= '0' && r <= '9' }
|
|
|
|
isNotNumeric := func(r rune) bool { return !isNumeric(r) }
|
|
|
|
|
|
|
|
minLength := len(a)
|
|
|
|
if len(b) < minLength {
|
|
|
|
minLength = len(b)
|
|
|
|
}
|
|
|
|
|
2020-12-14 20:30:48 +01:00
|
|
|
byteIndex := 0
|
2021-03-09 02:55:50 +01:00
|
|
|
numberStartIndex := -1
|
|
|
|
|
|
|
|
var aByte, bByte byte
|
|
|
|
|
|
|
|
// Start with a byte comparison to find where the strings differ.
|
|
|
|
for ; byteIndex < minLength; byteIndex++ {
|
|
|
|
aByte, bByte = a[byteIndex], b[byteIndex]
|
|
|
|
if aByte != bByte {
|
2020-12-14 20:30:48 +01:00
|
|
|
break
|
|
|
|
}
|
2021-03-09 02:55:50 +01:00
|
|
|
byteIsNumeric := isNumeric(rune(aByte))
|
|
|
|
if numberStartIndex != -1 && !byteIsNumeric {
|
|
|
|
numberStartIndex = -1
|
|
|
|
} else if numberStartIndex == -1 && byteIsNumeric {
|
|
|
|
numberStartIndex = byteIndex
|
|
|
|
}
|
2020-12-14 20:30:48 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 02:55:50 +01:00
|
|
|
// Handle the case where we reached the end of one or both strings without finding a difference.
|
|
|
|
if byteIndex == minLength {
|
|
|
|
if len(a) < len(b) {
|
|
|
|
// Reached the end of a. a is a prefix of b.
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
// Reached the end of b. b is a prefix of a or b is equal to a.
|
|
|
|
return false
|
|
|
|
}
|
2020-12-14 20:30:48 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 02:55:50 +01:00
|
|
|
aByteNumeric := isNumeric(rune(aByte))
|
|
|
|
bByteNumeric := isNumeric(rune(bByte))
|
2020-12-14 20:30:48 +01:00
|
|
|
|
2021-03-09 02:55:50 +01:00
|
|
|
if (aByteNumeric || bByteNumeric) && !(aByteNumeric && bByteNumeric) && numberStartIndex != -1 {
|
|
|
|
// Only one of aByte and bByte is a number, but the previous byte was a number. That means
|
|
|
|
// one is a longer number with the same prefix, which must be numerically larger. If bByte
|
|
|
|
// is a number then the number in b is numerically larger than the number in a.
|
|
|
|
return bByteNumeric
|
|
|
|
}
|
2020-12-14 20:30:48 +01:00
|
|
|
|
2021-03-09 02:55:50 +01:00
|
|
|
// If the bytes are both numbers do a numeric comparison.
|
|
|
|
if aByteNumeric && bByteNumeric {
|
|
|
|
// Extract the numbers from each string, starting from the first number after the last
|
|
|
|
// non-number. This won't be invalid utf8 because we are only looking for the bytes
|
|
|
|
//'0'-'9', which can only occur as single-byte runes in utf8.
|
|
|
|
if numberStartIndex == -1 {
|
|
|
|
numberStartIndex = byteIndex
|
|
|
|
}
|
|
|
|
aNumberString := a[numberStartIndex:]
|
|
|
|
bNumberString := b[numberStartIndex:]
|
2020-12-14 20:30:48 +01:00
|
|
|
|
|
|
|
// Find the first non-number in each, using the full length if there isn't one.
|
2021-03-09 02:55:50 +01:00
|
|
|
endANumbers := strings.IndexFunc(aNumberString, isNotNumeric)
|
|
|
|
endBNumbers := strings.IndexFunc(bNumberString, isNotNumeric)
|
2020-12-14 20:30:48 +01:00
|
|
|
if endANumbers == -1 {
|
2021-03-09 02:55:50 +01:00
|
|
|
endANumbers = len(aNumberString)
|
2020-12-14 20:30:48 +01:00
|
|
|
}
|
|
|
|
if endBNumbers == -1 {
|
2021-03-09 02:55:50 +01:00
|
|
|
endBNumbers = len(bNumberString)
|
2020-12-14 20:30:48 +01:00
|
|
|
}
|
2021-03-09 02:55:50 +01:00
|
|
|
|
2020-12-14 20:30:48 +01:00
|
|
|
// Convert each to an int.
|
2021-03-09 02:55:50 +01:00
|
|
|
aNumber, err := strconv.Atoi(aNumberString[:endANumbers])
|
2020-12-14 20:30:48 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to convert %q from %q to number: %w",
|
2021-03-09 02:55:50 +01:00
|
|
|
aNumberString[:endANumbers], a, err))
|
2020-12-14 20:30:48 +01:00
|
|
|
}
|
2021-03-09 02:55:50 +01:00
|
|
|
bNumber, err := strconv.Atoi(bNumberString[:endBNumbers])
|
2020-12-14 20:30:48 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to convert %q from %q to number: %w",
|
2021-03-09 02:55:50 +01:00
|
|
|
bNumberString[:endBNumbers], b, err))
|
2020-12-14 20:30:48 +01:00
|
|
|
}
|
|
|
|
// Do a numeric comparison.
|
|
|
|
return aNumber < bNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
// At least one is not a number, do a byte comparison.
|
2021-03-09 02:55:50 +01:00
|
|
|
return aByte < bByte
|
2020-12-14 20:30:48 +01:00
|
|
|
}
|
|
|
|
|
2015-01-09 04:35:10 +01:00
|
|
|
func SortLists(file *File) {
|
|
|
|
for _, def := range file.Defs {
|
|
|
|
if assignment, ok := def.(*Assignment); ok {
|
|
|
|
sortListsInValue(assignment.Value, file)
|
|
|
|
} else if module, ok := def.(*Module); ok {
|
|
|
|
for _, prop := range module.Properties {
|
|
|
|
sortListsInValue(prop.Value, file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Sort(commentsByOffset(file.Comments))
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
func SortList(file *File, list *List) {
|
Implement list of maps
Allow property value to be a list of maps, e.g.
my_module {
my_list: [
{ name: "foo", value: 42, something: true, },
{ name: "bar", value: 34, something: false, },
],
}
Test: internal
Change-Id: I2fc37d692aac39f23c9aa7bda2859ab49f3bc672
2020-02-12 07:39:47 +01:00
|
|
|
if !isListOfPrimitives(list.Values) {
|
|
|
|
return
|
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
for i := 0; i < len(list.Values); i++ {
|
2015-01-09 04:35:10 +01:00
|
|
|
// Find a set of values on contiguous lines
|
2016-06-07 21:28:16 +02:00
|
|
|
line := list.Values[i].Pos().Line
|
2015-01-09 04:35:10 +01:00
|
|
|
var j int
|
2016-06-07 21:28:16 +02:00
|
|
|
for j = i + 1; j < len(list.Values); j++ {
|
|
|
|
if list.Values[j].Pos().Line > line+1 {
|
2015-01-09 04:35:10 +01:00
|
|
|
break
|
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
line = list.Values[j].Pos().Line
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
nextPos := list.End()
|
|
|
|
if j < len(list.Values) {
|
|
|
|
nextPos = list.Values[j].Pos()
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
sortSubList(list.Values[i:j], nextPos, file)
|
2015-01-09 04:35:10 +01:00
|
|
|
i = j - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
func ListIsSorted(list *List) bool {
|
|
|
|
for i := 0; i < len(list.Values); i++ {
|
2015-01-09 04:35:10 +01:00
|
|
|
// Find a set of values on contiguous lines
|
2016-06-07 21:28:16 +02:00
|
|
|
line := list.Values[i].Pos().Line
|
2015-01-09 04:35:10 +01:00
|
|
|
var j int
|
2016-06-07 21:28:16 +02:00
|
|
|
for j = i + 1; j < len(list.Values); j++ {
|
|
|
|
if list.Values[j].Pos().Line > line+1 {
|
2015-01-09 04:35:10 +01:00
|
|
|
break
|
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
line = list.Values[j].Pos().Line
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
if !subListIsSorted(list.Values[i:j]) {
|
2015-01-09 04:35:10 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
i = j - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
func sortListsInValue(value Expression, file *File) {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case *Variable:
|
|
|
|
// Nothing
|
|
|
|
case *Operator:
|
|
|
|
sortListsInValue(v.Args[0], file)
|
|
|
|
sortListsInValue(v.Args[1], file)
|
|
|
|
case *Map:
|
|
|
|
for _, p := range v.Properties {
|
2015-01-09 04:35:10 +01:00
|
|
|
sortListsInValue(p.Value, file)
|
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
case *List:
|
|
|
|
SortList(file, v)
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
func sortSubList(values []Expression, nextPos scanner.Position, file *File) {
|
Implement list of maps
Allow property value to be a list of maps, e.g.
my_module {
my_list: [
{ name: "foo", value: 42, something: true, },
{ name: "bar", value: 34, something: false, },
],
}
Test: internal
Change-Id: I2fc37d692aac39f23c9aa7bda2859ab49f3bc672
2020-02-12 07:39:47 +01:00
|
|
|
if !isListOfPrimitives(values) {
|
|
|
|
return
|
|
|
|
}
|
2020-12-14 20:30:48 +01:00
|
|
|
l := make([]elem, len(values))
|
2015-01-09 04:35:10 +01:00
|
|
|
for i, v := range values {
|
2016-06-07 21:28:16 +02:00
|
|
|
s, ok := v.(*String)
|
|
|
|
if !ok {
|
2015-01-09 04:35:10 +01:00
|
|
|
panic("list contains non-string element")
|
|
|
|
}
|
|
|
|
n := nextPos
|
|
|
|
if i < len(values)-1 {
|
2016-06-07 21:28:16 +02:00
|
|
|
n = values[i+1].Pos()
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
l[i] = elem{s.Value, i, v.Pos(), n}
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
2020-12-14 20:30:48 +01:00
|
|
|
sort.SliceStable(l, func(i, j int) bool {
|
|
|
|
return numericStringLess(l[i].s, l[j].s)
|
|
|
|
})
|
2015-01-09 04:35:10 +01:00
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
copyValues := append([]Expression{}, values...)
|
2016-06-11 02:27:12 +02:00
|
|
|
copyComments := make([]*CommentGroup, len(file.Comments))
|
|
|
|
for i := range file.Comments {
|
|
|
|
cg := *file.Comments[i]
|
|
|
|
cg.Comments = make([]*Comment, len(cg.Comments))
|
|
|
|
for j := range file.Comments[i].Comments {
|
|
|
|
c := *file.Comments[i].Comments[j]
|
|
|
|
cg.Comments[j] = &c
|
|
|
|
}
|
|
|
|
copyComments[i] = &cg
|
|
|
|
}
|
2015-01-09 04:35:10 +01:00
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
curPos := values[0].Pos()
|
2015-01-09 04:35:10 +01:00
|
|
|
for i, e := range l {
|
|
|
|
values[i] = copyValues[e.i]
|
2016-06-07 21:28:16 +02:00
|
|
|
values[i].(*String).LiteralPos = curPos
|
2015-01-09 04:35:10 +01:00
|
|
|
for j, c := range copyComments {
|
2016-06-07 21:28:16 +02:00
|
|
|
if c.Pos().Offset > e.pos.Offset && c.Pos().Offset < e.nextPos.Offset {
|
2016-06-11 02:27:12 +02:00
|
|
|
file.Comments[j].Comments[0].Slash.Line = curPos.Line
|
|
|
|
file.Comments[j].Comments[0].Slash.Offset += values[i].Pos().Offset - e.pos.Offset
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curPos.Offset += e.nextPos.Offset - e.pos.Offset
|
|
|
|
curPos.Line++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
func subListIsSorted(values []Expression) bool {
|
Implement list of maps
Allow property value to be a list of maps, e.g.
my_module {
my_list: [
{ name: "foo", value: 42, something: true, },
{ name: "bar", value: 34, something: false, },
],
}
Test: internal
Change-Id: I2fc37d692aac39f23c9aa7bda2859ab49f3bc672
2020-02-12 07:39:47 +01:00
|
|
|
if !isListOfPrimitives(values) {
|
|
|
|
return true
|
|
|
|
}
|
2015-01-09 04:35:10 +01:00
|
|
|
prev := ""
|
|
|
|
for _, v := range values {
|
2016-06-07 21:28:16 +02:00
|
|
|
s, ok := v.(*String)
|
|
|
|
if !ok {
|
2015-01-09 04:35:10 +01:00
|
|
|
panic("list contains non-string element")
|
|
|
|
}
|
2020-12-14 20:30:48 +01:00
|
|
|
if prev != "" && numericStringLess(s.Value, prev) {
|
2015-01-09 04:35:10 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
prev = s.Value
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type elem struct {
|
|
|
|
s string
|
|
|
|
i int
|
|
|
|
pos scanner.Position
|
|
|
|
nextPos scanner.Position
|
|
|
|
}
|
|
|
|
|
2016-06-11 02:27:12 +02:00
|
|
|
type commentsByOffset []*CommentGroup
|
2015-01-09 04:35:10 +01:00
|
|
|
|
|
|
|
func (l commentsByOffset) Len() int {
|
|
|
|
return len(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l commentsByOffset) Less(i, j int) bool {
|
2016-06-07 21:28:16 +02:00
|
|
|
return l[i].Pos().Offset < l[j].Pos().Offset
|
2015-01-09 04:35:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l commentsByOffset) Swap(i, j int) {
|
|
|
|
l[i], l[j] = l[j], l[i]
|
|
|
|
}
|
Implement list of maps
Allow property value to be a list of maps, e.g.
my_module {
my_list: [
{ name: "foo", value: 42, something: true, },
{ name: "bar", value: 34, something: false, },
],
}
Test: internal
Change-Id: I2fc37d692aac39f23c9aa7bda2859ab49f3bc672
2020-02-12 07:39:47 +01:00
|
|
|
|
|
|
|
func isListOfPrimitives(values []Expression) bool {
|
|
|
|
if len(values) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
switch values[0].Type() {
|
|
|
|
case BoolType, StringType, Int64Type:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|