Commit f088840a authored by Daniel Clifford's avatar Daniel Clifford Committed by Commit Bot

[torque] Improve formatting in format-torque

Issues/problems addressed:

- Fix line-wrapping and indenting for long declarations including strings,
  e.g. generates and constexpr clauses.
- Implement proper formatting for typeswitch statements
- Fix formatting of operator declarations
- Fix formatting of constexpr if-clauses (the constexpr is now included on the
  same line as the if and it doesn't mess up the formatting that
- Fix formatting of label declarations on callables, the "label" keyword now
  always starts a new line with indentation.
- Remove space after identifier name in generic parameter declarations, e.g.
  "<a : T>" is now "<a: T>" which is consistent with type specification
  formatting elsewhere.
- Indent "otherwise" clauses that have been pushed to the next line.

Also ran the formatter over all existing .tq files.

Bug: v8:7793
Change-Id: I5adbb2ffa3d573deed062f9a5c1da57348c8fc71
Reviewed-on: https://chromium-review.googlesource.com/1238580
Commit-Queue: Daniel Clifford <danno@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56158}
parent c0f871a1
...@@ -72,10 +72,10 @@ module array { ...@@ -72,10 +72,10 @@ module array {
} }
} }
macro VisitAllElements<FixedArrayType : type>( macro VisitAllElements<FixedArrayType: type>(
context: Context, a: JSArray, len: Smi, callbackfn: Callable, context: Context, a: JSArray, len: Smi, callbackfn: Callable,
thisArg: Object): void labels thisArg: Object): void
Bailout(Smi) { labels Bailout(Smi) {
let k: Smi = 0; let k: Smi = 0;
const map: Map = a.map; const map: Map = a.map;
...@@ -108,8 +108,8 @@ module array { ...@@ -108,8 +108,8 @@ module array {
macro FastArrayForEach( macro FastArrayForEach(
context: Context, o: JSReceiver, len: Number, callbackfn: Callable, context: Context, o: JSReceiver, len: Number, callbackfn: Callable,
thisArg: Object): Object labels thisArg: Object): Object
Bailout(Smi) { labels Bailout(Smi) {
let k: Smi = 0; let k: Smi = 0;
try { try {
const smiLen: Smi = Cast<Smi>(len) otherwise Slow; const smiLen: Smi = Cast<Smi>(len) otherwise Slow;
...@@ -123,10 +123,10 @@ module array { ...@@ -123,10 +123,10 @@ module array {
if (IsElementsKindGreaterThan(elementsKind, HOLEY_ELEMENTS)) { if (IsElementsKindGreaterThan(elementsKind, HOLEY_ELEMENTS)) {
VisitAllElements<FixedDoubleArray>( VisitAllElements<FixedDoubleArray>(
context, a, smiLen, callbackfn, thisArg) context, a, smiLen, callbackfn, thisArg)
otherwise Bailout; otherwise Bailout;
} else { } else {
VisitAllElements<FixedArray>(context, a, smiLen, callbackfn, thisArg) VisitAllElements<FixedArray>(context, a, smiLen, callbackfn, thisArg)
otherwise Bailout; otherwise Bailout;
} }
} }
label Slow deferred { label Slow deferred {
...@@ -163,7 +163,7 @@ module array { ...@@ -163,7 +163,7 @@ module array {
let k: Number = 0; let k: Number = 0;
try { try {
return FastArrayForEach(context, o, len, callbackfn, thisArg) return FastArrayForEach(context, o, len, callbackfn, thisArg)
otherwise Bailout; otherwise Bailout;
} }
label Bailout(kValue: Smi) deferred { label Bailout(kValue: Smi) deferred {
k = kValue; k = kValue;
......
...@@ -3,28 +3,28 @@ ...@@ -3,28 +3,28 @@
// found in the LICENSE file. // found in the LICENSE file.
module array { module array {
macro LoadWithHoleCheck<Elements : type>( macro LoadWithHoleCheck<Elements: type>(
elements: FixedArrayBase, index: Smi): Object elements: FixedArrayBase, index: Smi): Object
labels IfHole; labels IfHole;
LoadWithHoleCheck<FixedArray>(elements: FixedArrayBase, index: Smi): Object LoadWithHoleCheck<FixedArray>(elements: FixedArrayBase, index: Smi): Object
labels IfHole { labels IfHole {
const elements: FixedArray = UnsafeCast<FixedArray>(elements); const elements: FixedArray = UnsafeCast<FixedArray>(elements);
const element: Object = elements[index]; const element: Object = elements[index];
if (element == Hole) goto IfHole; if (element == Hole) goto IfHole;
return element; return element;
} }
LoadWithHoleCheck<FixedDoubleArray>( LoadWithHoleCheck<FixedDoubleArray>(elements: FixedArrayBase, index: Smi):
elements: FixedArrayBase, index: Smi): Object Object
labels IfHole { labels IfHole {
const elements: FixedDoubleArray = UnsafeCast<FixedDoubleArray>(elements); const elements: FixedDoubleArray = UnsafeCast<FixedDoubleArray>(elements);
const element: float64 = LoadDoubleWithHoleCheck(elements, index) const element: float64 = LoadDoubleWithHoleCheck(elements, index)
otherwise IfHole; otherwise IfHole;
return AllocateHeapNumberWithValue(element); return AllocateHeapNumberWithValue(element);
} }
macro FastArrayLastIndexOf<Elements : type>( macro FastArrayLastIndexOf<Elements: type>(
context: Context, array: JSArray, length: Smi, from: Smi, context: Context, array: JSArray, length: Smi, from: Smi,
searchElement: Object): Smi { searchElement: Object): Smi {
const elements: FixedArrayBase = array.elements; const elements: FixedArrayBase = array.elements;
...@@ -32,7 +32,7 @@ module array { ...@@ -32,7 +32,7 @@ module array {
while (k >= 0) { while (k >= 0) {
try { try {
const element: Object = LoadWithHoleCheck<Elements>(elements, k) const element: Object = LoadWithHoleCheck<Elements>(elements, k)
otherwise Hole; otherwise Hole;
const same: Boolean = StrictEqual(searchElement, element); const same: Boolean = StrictEqual(searchElement, element);
if (same == True) { if (same == True) {
...@@ -74,7 +74,7 @@ module array { ...@@ -74,7 +74,7 @@ module array {
macro TryFastArrayLastIndexOf( macro TryFastArrayLastIndexOf(
context: Context, receiver: JSReceiver, searchElement: Object, context: Context, receiver: JSReceiver, searchElement: Object,
from: Number): Object from: Number): Object
labels Slow { labels Slow {
EnsureFastJSArray(context, receiver) otherwise Slow; EnsureFastJSArray(context, receiver) otherwise Slow;
const array: JSArray = UnsafeCast<JSArray>(receiver); const array: JSArray = UnsafeCast<JSArray>(receiver);
...@@ -142,7 +142,7 @@ module array { ...@@ -142,7 +142,7 @@ module array {
try { try {
return TryFastArrayLastIndexOf(context, object, searchElement, from) return TryFastArrayLastIndexOf(context, object, searchElement, from)
otherwise Baseline; otherwise Baseline;
} }
label Baseline { label Baseline {
return GenericArrayLastIndexOf(context, object, searchElement, from); return GenericArrayLastIndexOf(context, object, searchElement, from);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
module array { module array {
macro LoadElement<ElementsAccessor : type, T : type>( macro LoadElement<ElementsAccessor: type, T: type>(
elements: FixedArrayBase, index: Smi): T; elements: FixedArrayBase, index: Smi): T;
LoadElement<FastPackedSmiElements, Smi>( LoadElement<FastPackedSmiElements, Smi>(
...@@ -31,7 +31,7 @@ module array { ...@@ -31,7 +31,7 @@ module array {
} }
} }
macro StoreElement<ElementsAccessor : type, T : type>( macro StoreElement<ElementsAccessor: type, T: type>(
elements: FixedArrayBase, index: Smi, value: T); elements: FixedArrayBase, index: Smi, value: T);
StoreElement<FastPackedSmiElements, Smi>( StoreElement<FastPackedSmiElements, Smi>(
...@@ -57,7 +57,7 @@ module array { ...@@ -57,7 +57,7 @@ module array {
// Fast-path for all PACKED_* elements kinds. These do not need to check // Fast-path for all PACKED_* elements kinds. These do not need to check
// whether a property is present, so we can simply swap them using fast // whether a property is present, so we can simply swap them using fast
// FixedArray loads/stores. // FixedArray loads/stores.
macro FastPackedArrayReverse<Accessor : type, T : type>( macro FastPackedArrayReverse<Accessor: type, T: type>(
elements: FixedArrayBase, length: Smi) { elements: FixedArrayBase, length: Smi) {
let lower: Smi = 0; let lower: Smi = 0;
let upper: Smi = length - 1; let upper: Smi = length - 1;
......
...@@ -7,7 +7,7 @@ module array { ...@@ -7,7 +7,7 @@ module array {
// FixedArrayType. Most of this behavior is outsourced to ExtractFixedArray(), // FixedArrayType. Most of this behavior is outsourced to ExtractFixedArray(),
// but the special case of wanting to have a FixedDoubleArray when given a // but the special case of wanting to have a FixedDoubleArray when given a
// zero-length input FixedArray is handled here. // zero-length input FixedArray is handled here.
macro Extract<FixedArrayType : type>( macro Extract<FixedArrayType: type>(
elements: FixedArrayBase, first: Smi, count: Smi, elements: FixedArrayBase, first: Smi, count: Smi,
capacity: Smi): FixedArrayType { capacity: Smi): FixedArrayType {
return UnsafeCast<FixedArrayType>( return UnsafeCast<FixedArrayType>(
...@@ -24,10 +24,11 @@ module array { ...@@ -24,10 +24,11 @@ module array {
ExtractFixedArray(elements, first, count, capacity)); ExtractFixedArray(elements, first, count, capacity));
} }
macro FastSplice<FixedArrayType : type, ElementType : type>( macro FastSplice<FixedArrayType: type, ElementType: type>(
args: constexpr Arguments, a: JSArray, length: Smi, newLength: Smi, args: constexpr Arguments, a: JSArray, length: Smi, newLength: Smi,
lengthDelta: Smi, actualStart: Smi, insertCount: Smi, lengthDelta: Smi, actualStart: Smi, insertCount: Smi,
actualDeleteCount: Smi): void labels Bailout { actualDeleteCount: Smi): void
labels Bailout {
const elements: FixedArrayBase = a.elements; const elements: FixedArrayBase = a.elements;
const elementsMap: Map = elements.map; const elementsMap: Map = elements.map;
...@@ -88,7 +89,7 @@ module array { ...@@ -88,7 +89,7 @@ module array {
context: Context, args: constexpr Arguments, o: JSReceiver, context: Context, args: constexpr Arguments, o: JSReceiver,
originalLengthNumber: Number, actualStartNumber: Number, insertCount: Smi, originalLengthNumber: Number, actualStartNumber: Number, insertCount: Smi,
actualDeleteCountNumber: Number): Object actualDeleteCountNumber: Number): Object
labels Bailout { labels Bailout {
const originalLength: Smi = const originalLength: Smi =
Cast<Smi>(originalLengthNumber) otherwise Bailout; Cast<Smi>(originalLengthNumber) otherwise Bailout;
const actualStart: Smi = Cast<Smi>(actualStartNumber) otherwise Bailout; const actualStart: Smi = Cast<Smi>(actualStartNumber) otherwise Bailout;
......
...@@ -6,8 +6,9 @@ module array { ...@@ -6,8 +6,9 @@ module array {
extern builtin ArrayUnshift(Context, JSFunction, Object, int32); extern builtin ArrayUnshift(Context, JSFunction, Object, int32);
macro TryFastArrayUnshift( macro TryFastArrayUnshift(
context: Context, receiver: Object, arguments: constexpr Arguments): never context: Context, receiver: Object, arguments: constexpr Arguments):
labels Slow { never
labels Slow {
EnsureFastJSArray(context, receiver) otherwise Slow; EnsureFastJSArray(context, receiver) otherwise Slow;
const array: JSArray = UnsafeCast<JSArray>(receiver); const array: JSArray = UnsafeCast<JSArray>(receiver);
EnsureWriteableFastElements(array); EnsureWriteableFastElements(array);
......
...@@ -35,8 +35,8 @@ module array { ...@@ -35,8 +35,8 @@ module array {
assert(IsFastSmiOrTaggedElementsKind(array.map.elements_kind)); assert(IsFastSmiOrTaggedElementsKind(array.map.elements_kind));
const length: Smi = array.length_fast; const length: Smi = array.length_fast;
array.elements = ExtractFixedArray( array.elements =
elements, 0, length, length, kFixedArrays); ExtractFixedArray(elements, 0, length, length, kFixedArrays);
assert(array.elements.map != kCOWMap); assert(array.elements.map != kCOWMap);
} }
...@@ -69,7 +69,7 @@ module array { ...@@ -69,7 +69,7 @@ module array {
to: Smi): void { to: Smi): void {
try { try {
const floatValue: float64 = LoadDoubleWithHoleCheck(elements, from) const floatValue: float64 = LoadDoubleWithHoleCheck(elements, from)
otherwise FoundHole; otherwise FoundHole;
newElements[to] = floatValue; newElements[to] = floatValue;
} }
label FoundHole { label FoundHole {
......
This diff is collapsed.
This diff is collapsed.
...@@ -1265,7 +1265,7 @@ struct TorqueGrammar : Grammar { ...@@ -1265,7 +1265,7 @@ struct TorqueGrammar : Grammar {
Symbol typeswitchCase = { Symbol typeswitchCase = {
Rule({Token("case"), Token("("), Rule({Token("case"), Token("("),
Optional<std::string>(Sequence({&identifier, Token(":")})), &type, Optional<std::string>(Sequence({&identifier, Token(":")})), &type,
Token(")"), &block}, Token(")"), Token(":"), &block},
MakeTypeswitchCase)}; MakeTypeswitchCase)};
// Result: base::Optional<Statement*> // Result: base::Optional<Statement*>
......
...@@ -4,10 +4,9 @@ ...@@ -4,10 +4,9 @@
module test { module test {
macro ElementsKindTestHelper1(kind: constexpr ElementsKind): bool { macro ElementsKindTestHelper1(kind: constexpr ElementsKind): bool {
if constexpr((kind == UINT8_ELEMENTS) || (kind == UINT16_ELEMENTS)) { if constexpr ((kind == UINT8_ELEMENTS) || (kind == UINT16_ELEMENTS)) {
return true; return true;
} } else {
else {
return false; return false;
} }
} }
...@@ -21,17 +20,17 @@ module test { ...@@ -21,17 +20,17 @@ module test {
} }
macro LabelTestHelper1(): never macro LabelTestHelper1(): never
labels Label1 { labels Label1 {
goto Label1; goto Label1;
} }
macro LabelTestHelper2(): never macro LabelTestHelper2(): never
labels Label2(Smi) { labels Label2(Smi) {
goto Label2(42); goto Label2(42);
} }
macro LabelTestHelper3(): never macro LabelTestHelper3(): never
labels Label3(String, Smi) { labels Label3(String, Smi) {
goto Label3('foo', 7); goto Label3('foo', 7);
} }
...@@ -82,7 +81,7 @@ module test { ...@@ -82,7 +81,7 @@ module test {
} }
} }
builtin GenericBuiltinTest<T : type>(c: Context, param: T): Object { builtin GenericBuiltinTest<T: type>(c: Context, param: T): Object {
return Null; return Null;
} }
...@@ -97,8 +96,9 @@ module test { ...@@ -97,8 +96,9 @@ module test {
check(GenericBuiltinTest<Object>(c, Undefined) == Undefined); check(GenericBuiltinTest<Object>(c, Undefined) == Undefined);
} }
macro LabelTestHelper4(flag: constexpr bool): never labels Label4, Label5 { macro LabelTestHelper4(flag: constexpr bool): never
if constexpr(flag) { labels Label4, Label5 {
if constexpr (flag) {
goto Label4; goto Label4;
} else { } else {
goto Label5; goto Label5;
...@@ -128,7 +128,7 @@ module test { ...@@ -128,7 +128,7 @@ module test {
} }
} }
macro GenericMacroTest<T : type>(param: T): Object { macro GenericMacroTest<T: type>(param: T): Object {
return Undefined; return Undefined;
} }
...@@ -136,11 +136,13 @@ module test { ...@@ -136,11 +136,13 @@ module test {
return param2; return param2;
} }
macro GenericMacroTestWithLabels<T : type>(param: T): Object labels X { macro GenericMacroTestWithLabels<T: type>(param: T): Object
labels X {
return Undefined; return Undefined;
} }
GenericMacroTestWithLabels<Object>(param2: Object): Object labels Y { GenericMacroTestWithLabels<Object>(param2: Object): Object
labels Y {
return param2; return param2;
} }
...@@ -244,12 +246,12 @@ module test { ...@@ -244,12 +246,12 @@ module test {
} }
macro TestLocalConstBindings() { macro TestLocalConstBindings() {
const x : constexpr int31 = 3; const x: constexpr int31 = 3;
const xSmi : Smi = x; const xSmi: Smi = x;
{ {
const x : Smi = x + FromConstexpr<Smi>(1); const x: Smi = x + FromConstexpr<Smi>(1);
check(x == xSmi + 1); check(x == xSmi + 1);
const xSmi : Smi = x; const xSmi: Smi = x;
check(x == xSmi); check(x == xSmi);
check(x == 4); check(x == 4);
} }
...@@ -287,14 +289,15 @@ module test { ...@@ -287,14 +289,15 @@ module test {
d.x = a; d.x = a;
d = TestStructB{a, 7}; d = TestStructB{a, 7};
let e: TestStructA = d.x; let e: TestStructA = d.x;
let f: Smi = TestStructA{UnsafeCast<FixedArray>(kEmptyFixedArray), 27, 31}.i; let f: Smi =
TestStructA{UnsafeCast<FixedArray>(kEmptyFixedArray), 27, 31}.i;
f = TestStruct2().i; f = TestStruct2().i;
return a; return a;
} }
struct TestStructC { struct TestStructC {
x : TestStructA; x: TestStructA;
y : TestStructA; y: TestStructA;
} }
macro TestStruct4(): TestStructC { macro TestStruct4(): TestStructC {
...@@ -387,38 +390,42 @@ module test { ...@@ -387,38 +390,42 @@ module test {
check(sum == 7); check(sum == 7);
} }
macro TestSubtyping(x : Smi) { macro TestSubtyping(x: Smi) {
const foo : Object = x; const foo: Object = x;
} }
macro IncrementIfSmi<A : type>(x : A) : A { macro IncrementIfSmi<A: type>(x: A): A {
typeswitch (x) { typeswitch (x) {
case (x : Smi) { case (x: Smi): {
return x + 1; return x + 1;
} case (o : A) { }
case (o: A): {
return o; return o;
} }
} }
} }
macro TypeswitchExample(x : Number | FixedArray) : int32 { macro TypeswitchExample(x: Number | FixedArray): int32 {
let result : int32 = 0; let result: int32 = 0;
typeswitch (IncrementIfSmi<(Number|FixedArray)>(x)) { typeswitch (IncrementIfSmi<(Number | FixedArray)>(x)) {
case (x : FixedArray) { case (x: FixedArray): {
result = result + 1; result = result + 1;
} case (Number) { }
case (Number): {
result = result + 2; result = result + 2;
} }
} }
result = result * 10; result = result * 10;
typeswitch (IncrementIfSmi<(Number|FixedArray)>(x)) { typeswitch (IncrementIfSmi<(Number | FixedArray)>(x)) {
case (x : Smi) { case (x: Smi): {
result = result + Convert<int32>(x); result = result + Convert<int32>(x);
} case (a : FixedArray) { }
case (a: FixedArray): {
result = result + Convert<int32>(a.length); result = result + Convert<int32>(a.length);
} case (x : HeapNumber) { }
case (x: HeapNumber): {
result = result + 7; result = result + 7;
} }
} }
...@@ -428,26 +435,27 @@ module test { ...@@ -428,26 +435,27 @@ module test {
macro TestTypeswitch() { macro TestTypeswitch() {
check(TypeswitchExample(FromConstexpr<Smi>(5)) == 26); check(TypeswitchExample(FromConstexpr<Smi>(5)) == 26);
const a : FixedArray = AllocateZeroedFixedArray(3); const a: FixedArray = AllocateZeroedFixedArray(3);
check(TypeswitchExample(a) == 13); check(TypeswitchExample(a) == 13);
check(TypeswitchExample(FromConstexpr<Number>(0.5)) == 27); check(TypeswitchExample(FromConstexpr<Number>(0.5)) == 27);
} }
macro ExampleGenericOverload<A: type>(o : Object) : A { macro ExampleGenericOverload<A: type>(o: Object): A {
return o; return o;
} }
macro ExampleGenericOverload<A: type>(o : Smi) : A { macro ExampleGenericOverload<A: type>(o: Smi): A {
return o + 1; return o + 1;
} }
macro TestGenericOverload() { macro TestGenericOverload() {
const xSmi : Smi = 5; const xSmi: Smi = 5;
const xObject : Object = xSmi; const xObject: Object = xSmi;
check(ExampleGenericOverload<Smi>(xSmi) == 6); check(ExampleGenericOverload<Smi>(xSmi) == 6);
check(UnsafeCast<Smi>(ExampleGenericOverload<Object>(xObject)) == 5); check(UnsafeCast<Smi>(ExampleGenericOverload<Object>(xObject)) == 5);
} }
macro BoolToBranch(x : bool) : never labels Taken, NotTaken { macro BoolToBranch(x: bool): never
labels Taken, NotTaken {
if (x) { if (x) {
goto Taken; goto Taken;
} else { } else {
...@@ -455,27 +463,27 @@ module test { ...@@ -455,27 +463,27 @@ module test {
} }
} }
macro TestOrAnd1(x : bool, y : bool, z : bool) : bool { macro TestOrAnd1(x: bool, y: bool, z: bool): bool {
return BoolToBranch(x) || y && z ? true : false; return BoolToBranch(x) || y && z ? true : false;
} }
macro TestOrAnd2(x : bool, y : bool, z : bool) : bool { macro TestOrAnd2(x: bool, y: bool, z: bool): bool {
return x || BoolToBranch(y) && z ? true : false; return x || BoolToBranch(y) && z ? true : false;
} }
macro TestOrAnd3(x : bool, y : bool, z : bool) : bool { macro TestOrAnd3(x: bool, y: bool, z: bool): bool {
return x || y && BoolToBranch(z) ? true : false; return x || y && BoolToBranch(z) ? true : false;
} }
macro TestAndOr1(x : bool, y : bool, z : bool) : bool { macro TestAndOr1(x: bool, y: bool, z: bool): bool {
return BoolToBranch(x) && y || z ? true : false; return BoolToBranch(x) && y || z ? true : false;
} }
macro TestAndOr2(x : bool, y : bool, z : bool) : bool { macro TestAndOr2(x: bool, y: bool, z: bool): bool {
return x && BoolToBranch(y) || z ? true : false; return x && BoolToBranch(y) || z ? true : false;
} }
macro TestAndOr3(x : bool, y : bool, z : bool) : bool { macro TestAndOr3(x: bool, y: bool, z: bool): bool {
return x && y || BoolToBranch(z) ? true : false; return x && y || BoolToBranch(z) ? true : false;
} }
...@@ -529,5 +537,4 @@ module test { ...@@ -529,5 +537,4 @@ module test {
check(!TestOrAnd2(false, false, false)); check(!TestOrAnd2(false, false, false));
check(!TestOrAnd3(false, false, false)); check(!TestOrAnd3(false, false, false));
} }
} }
This diff is collapsed.
...@@ -11,6 +11,42 @@ import sys ...@@ -11,6 +11,42 @@ import sys
import re import re
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
def preprocess(input):
input = re.sub(r'(if\s+)constexpr(\s*\()', r'\1/*COxp*/\2', input)
input = re.sub(r'(\)\s*\:\s*\S+\s+)labels\s+',
r'\1,\n/*_LABELS_HOLD_*/ ', input)
input = re.sub(r'(\s+)operator\s*(\'[^\']+\')', r'\1/*_OPE \2*/', input)
input = re.sub(r'(\s+)typeswitch\s*\(', r'\1/*_TYPE*/switch (', input)
input = re.sub(r'(\s+)case\s*\(([^\s]+)\s+\:\s*([^\:]+)\)(\s*)\:',
r'\1case \3: /*_TSV\2:*/', input)
input = re.sub(r'(\s+)case\s*\(([^\:]+)\)(\s*)\:',
r'\1case \2: /*_TSX*/', input)
input = re.sub(r'\sgenerates\s+\'([^\']+)\'\s*',
r' _GeNeRaT_/*\1@*/', input)
input = re.sub(r'\sconstexpr\s+\'([^\']+)\'\s*',
r' _CoNsExP_/*\1@*/', input)
return input
def postprocess(output):
output = re.sub(r'\/\*COxp\*\/', r'constexpr', output)
output = re.sub(r'(\S+)\s*: type([,>])', r'\1: type\2', output)
output = re.sub(r',([\n ]*)\/\*_LABELS_HOLD_\*\/', r'\1labels', output)
output = re.sub(r'\/\*_OPE \'([^\']+)\'\*\/', r"operator '\1'", output)
output = re.sub(r'\/\*_TYPE\*\/(\s*)switch', r'typeswitch', output)
output = re.sub(r'case ([^\:]+)\:\s*\/\*_TSX\*\/',
r'case (\1):', output)
output = re.sub(r'case ([^\:]+)\:\s*\/\*_TSV([^\:]+)\:\*\/',
r'case (\2: \1):', output)
output = re.sub(r'(\n\s*)_GeNeRaT_\s*\/\*([^@]+)@\*\/',
r"\1 generates '\2'", output)
output = re.sub(r'_GeNeRaT_\s*\/\*([^@]+)@\*\/',
r"generates '\1'", output)
output = re.sub(r'_CoNsExP_\s*\/\*([^@]+)@\*\/',
r"constexpr '\1'", output)
output = re.sub(r'(\n\s*)otherwise(\s+\S+\s*[\(\;])',
r"\1 otherwise\2", output)
return output
if len(sys.argv) < 2 or len(sys.argv) > 3: if len(sys.argv) < 2 or len(sys.argv) > 3:
print "invalid number of arguments" print "invalid number of arguments"
sys.exit(-1) sys.exit(-1)
...@@ -24,7 +60,8 @@ filename = sys.argv[len(sys.argv) - 1] ...@@ -24,7 +60,8 @@ filename = sys.argv[len(sys.argv) - 1]
with open(filename, 'r') as content_file: with open(filename, 'r') as content_file:
content = content_file.read() content = content_file.read()
p = Popen(['clang-format', '-assume-filename=.ts'], stdin=PIPE, stdout=PIPE, stderr=PIPE) p = Popen(['clang-format', '-assume-filename=.ts'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(content) output, err = p.communicate(preprocess(content))
output = postprocess(output)
rc = p.returncode rc = p.returncode
if (rc <> 0): if (rc <> 0):
sys.exit(rc); sys.exit(rc);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment