Skip to content

Commit 658e48c

Browse files
authored
Merge pull request #54 from tammela/fixes
feature: add string literal transformation
2 parents 03bc9d1 + b48f4b0 commit 658e48c

17 files changed

+183
-43
lines changed

.travis.yml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
language: cpp
2-
sudo: false
32

43
matrix:
54
include:
65

76
# linux gcc
87
- os: linux
9-
addons:
10-
apt:
11-
sources:
12-
- ubuntu-toolchain-r-test
13-
packages:
14-
- ['g++-9']
158
env:
169
- COMPILER='g++-9'
1710

1811
# os X
1912
- os: osx
20-
osx_image: xcode11.2
13+
osx_image: xcode11
2114
compiler: clang
22-
env: COMPILER='clang++'
15+
env:
16+
- COMPILER='clang++'
17+
- MYLDFLAGS='-L/usr/local/opt/llvm/lib'
18+
- MYCXXFLAGS='-I/usr/local/opt/llvm/include'
19+
- MYPATH='/usr/local/opt/llvm/bin'
2320

2421

2522
addons:
2623
homebrew:
2724
packages:
2825
- lcov
26+
- llvm
2927
update: true
3028
apt:
29+
sources:
30+
- sourceline: 'ppa:ubuntu-toolchain-r/test'
3131
packages:
3232
- lcov
33+
- g++-9
3334
update: true
3435

3536
before_install:
37+
- export PATH="${MYPATH}:$PATH"
3638
- export CXX=${COMPILER}
39+
- export LDFLAGS=${MYLDFLAGS}
40+
- export CXXFLAGS=${MYCXXFLAGS}
3741
- git submodule update --init
3842

3943
install:
40-
- cmake . -BRelease -DCMAKE_BUILD_TYPE=Release
41-
- cmake . -BDebug -DCMAKE_BUILD_TYPE=Debug
44+
- cmake -E env LDFLAGS="$LDFLAGS" CXXFLAGS="$CXXFLAGS" cmake . -BRelease -DCMAKE_BUILD_TYPE=Release
45+
- cmake -E env LDFLAGS="$LDFLAGS" CXXFLAGS="$CXXFLAGS" cmake . -BDebug -DCMAKE_BUILD_TYPE=Debug
4246

4347
script:
4448
- cd Debug

CMakeLists.txt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
cmake_minimum_required(VERSION 3.8)
1+
cmake_minimum_required(VERSION 3.9)
2+
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment version")
23

34
project("lua-formatter" VERSION 1.3.0 LANGUAGES CXX)
45

@@ -9,6 +10,16 @@ set(CMAKE_CXX_STANDARD 17)
910
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1011
set(CMAKE_CXX_EXTENSIONS OFF)
1112

13+
include(CheckIPOSupported)
14+
check_ipo_supported(RESULT lto OUTPUT error)
15+
16+
if(lto)
17+
message(STATUS "IPO / LTO enabled")
18+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
19+
else()
20+
message(STATUS "IPO / LTO not supported: <${error}>")
21+
endif()
22+
1223
if(NOT CMAKE_BUILD_TYPE)
1324
message("set build type to Release.")
1425
set(CMAKE_BUILD_TYPE Release)
@@ -17,8 +28,6 @@ endif()
1728
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -Wall")
1829
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
1930

20-
cmake_policy(SET CMP0067 NEW)
21-
2231
try_run(TEST_RUN_RESULT
2332
TEST_COMPILE_RESULT
2433
${CMAKE_CURRENT_BINARY_DIR}/

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ luarocks install --server=https://luarocks.org/dev luaformatter
2525
### Build from source
2626

2727
#### Requirements
28-
* cmake 3.8+
28+
* cmake 3.9+
2929
* c++ 17 compiler
3030

3131
#### Steps
@@ -88,6 +88,8 @@ chop_down_kv_table: true
8888
table_sep: ","
8989
extra_sep_at_table_end: false
9090
break_after_operator: true
91+
double_quote_to_single_quote: false
92+
single_quote_to_double_quote: false
9193
```
9294
## Limitations
9395

docs/Style-Config.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,46 @@ x = 11111 + 11111
356356
+ 11111
357357
```
358358

359+
### single_quote_to_double_quote
359360

361+
type: bool, default: false
362+
363+
Transform string literals to use double quote.
364+
365+
```lua
366+
-- original
367+
local foo = 'a'
368+
local foo = '"'
369+
local bar = 'don\'t'
370+
local foo = '\''
371+
local foobar = '\\\\\''
372+
373+
-- transformed
374+
local foo = "a"
375+
local foo = "\""
376+
local bar = "don't"
377+
local foo = "'"
378+
local foo = "\\\\'"
379+
```
380+
381+
### double_quote_to_single_quote
382+
383+
type: bool, default: false
384+
385+
Transform string literals to use single quote.
386+
387+
```lua
388+
-- original
389+
local foo = "a"
390+
local foo = "'"
391+
local bar = "don't"
392+
local bar = "\""
393+
local foobar = "\\\\\""
394+
395+
-- transformed
396+
local foo = 'a'
397+
local foo = '\''
398+
local bar = 'don\'t'
399+
local bar = '"'
400+
local foobar = '\\\\"'
401+
```

luaformatter-scm-1.rockspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
rockspec_format = "3.0"
12
package = "LuaFormatter"
23
version = "scm-1"
34
source = {
4-
url = "git+https://github.com/Koihik/LuaFormatter.git"
5+
url = "git://github.com/Koihik/LuaFormatter.git"
56
}
67
description = {
78
summary = "Reformats your Lua source code.",

src/Config.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#include "Config.h"
22

33
#include <fstream>
4+
#include <iostream>
5+
#include <filesystem>
6+
#include <cstdlib>
7+
8+
namespace fs = filesystem;
49

510
Config::Config() {
611
// Defaul configuration
@@ -29,9 +34,25 @@ Config::Config() {
2934
node_["extra_sep_at_table_end"] = false;
3035

3136
node_["break_after_operator"] = true;
37+
38+
node_["double_quote_to_single_quote"] = false;
39+
node_["single_quote_to_double_quote"] = false;
3240
}
3341

3442
void Config::readFromFile(const string& file) {
43+
fs::file_status status = fs::status(file);
44+
fs::perms perm = status.permissions();
45+
46+
if (!fs::is_regular_file(status)) {
47+
cerr << file << ": Not a file." << endl;
48+
exit(-1);
49+
}
50+
51+
if ((perm & fs::perms::owner_read) == fs::perms::none) {
52+
cerr << file << ": No access to read." << endl;
53+
exit(-1);
54+
}
55+
3556
YAML::Node n = YAML::LoadFile(file);
3657

3758
// Keys are always strings
@@ -43,6 +64,6 @@ void Config::readFromFile(const string& file) {
4364
}
4465
}
4566

46-
void Config::dumpCurrent(ofstream& fout) { fout << node_; }
67+
void Config::dumpCurrent(ofstream& fout) { fout << node_ << endl; }
4768

48-
void Config::dumpCurrent(ostream& out) { out << node_; }
69+
void Config::dumpCurrent(ostream& out) { out << node_ << endl; }

src/FormatVisitor.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "FormatVisitor.h"
22

33
#include <iostream>
4+
#include <regex>
45

56
#include "LuaLexer.h"
67

@@ -993,13 +994,61 @@ antlrcpp::Any FormatVisitor::visitExp(LuaParser::ExpContext* ctx) {
993994
visitFunctiondef(ctx->functiondef());
994995
} else if (ctx->tableconstructor() != NULL) {
995996
visitTableconstructor(ctx->tableconstructor());
997+
} else if (ctx->string() != NULL) {
998+
visitString(ctx->string());
996999
} else {
9971000
cur_writer() << ctx->getText();
9981001
}
9991002
LOG_FUNCTION_END("visitExp");
10001003
return nullptr;
10011004
}
10021005

1006+
antlrcpp::Any FormatVisitor::visitString(LuaParser::StringContext *ctx) {
1007+
if (ctx->NORMALSTRING() || ctx->CHARSTRING()) {
1008+
char quote = ctx->NORMALSTRING() ? '\'' : '\"';
1009+
tree::TerminalNode *tn;
1010+
1011+
switch (quote) {
1012+
case '\"':
1013+
if (!config_.get<bool>("single_quote_to_double_quote")) goto out;
1014+
tn = ctx->CHARSTRING();
1015+
break;
1016+
case '\'':
1017+
if (!config_.get<bool>("double_quote_to_single_quote")) goto out;
1018+
tn = ctx->NORMALSTRING();
1019+
break;
1020+
default:
1021+
goto out;
1022+
}
1023+
1024+
string newstr = tn->getSymbol()->getText();
1025+
1026+
regex re_single("'", regex_constants::extended);
1027+
regex re_double("\"", regex_constants::extended);
1028+
regex re_escapedsingle("\\\\'", regex_constants::extended);
1029+
regex re_escapeddouble("\\\\\"", regex_constants::extended);
1030+
1031+
if (quote == '\"') {
1032+
newstr = regex_replace(newstr, re_escapedsingle, "'");
1033+
newstr = regex_replace(newstr, re_double, "\\\"");
1034+
} else {
1035+
newstr = regex_replace(newstr, re_single, "\\'");
1036+
newstr = regex_replace(newstr, re_escapeddouble, "\"");
1037+
}
1038+
1039+
// switch the beginning and end to the new format
1040+
*newstr.begin() = quote;
1041+
*newstr.rbegin() = quote;
1042+
1043+
cur_writer() << newstr;
1044+
return nullptr;
1045+
}
1046+
1047+
out:
1048+
cur_writer() << ctx->getText();
1049+
return nullptr;
1050+
}
1051+
10031052
// varOrExp nameAndArgs*;
10041053
antlrcpp::Any FormatVisitor::visitPrefixexp(LuaParser::PrefixexpContext* ctx) {
10051054
LOG_FUNCTION_BEGIN("visitPrefixexp");

src/FormatVisitor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class FormatVisitor : public LuaBaseVisitor {
3838
antlrcpp::Any visitExplist(LuaParser::ExplistContext* context) override;
3939

4040
antlrcpp::Any visitExp(LuaParser::ExpContext* context) override;
41+
antlrcpp::Any visitString(LuaParser::StringContext *ctx) override;
4142
antlrcpp::Any visitPrefixexp(LuaParser::PrefixexpContext* context) override;
4243
antlrcpp::Any visitVarOrExp(LuaParser::VarOrExpContext* context) override;
4344
antlrcpp::Any visitVar(LuaParser::VarContext* context) override;
@@ -103,4 +104,4 @@ class FormatVisitor : public LuaBaseVisitor {
103104
void decContinuationIndent();
104105
void incIndentForAlign(int indent);
105106
void decIndentForAlign(int indent);
106-
};
107+
};

src/lua-format.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
using namespace std;
77
using namespace antlr4;
88

9-
string __format(ANTLRInputStream& input, const Config& config) {
9+
static string __format(ANTLRInputStream& input, const Config& config) {
1010
LuaLexer lexer(&input);
11-
CommonTokenStream tokens(&lexer);
12-
LuaParser parser(&tokens);
11+
CommonTokenStream tokenstream(&lexer);
12+
LuaParser parser(&tokenstream);
1313

1414
LuaParser::ChunkContext* chunk = parser.chunk();
1515

@@ -18,7 +18,7 @@ string __format(ANTLRInputStream& input, const Config& config) {
1818
}
1919

2020
vector<antlr4::Token*> tokenVector;
21-
for (auto t : tokens.getTokens()) {
21+
for (auto t : tokenstream.getTokens()) {
2222
tokenVector.emplace_back(t);
2323
}
2424

src/main.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,6 @@ int main(int argc, const char* argv[]) {
5656
}
5757

5858
if (fs::exists(configFileName)) {
59-
fs::file_status status = fs::status(configFileName);
60-
fs::perms perm = status.permissions();
61-
62-
if (!fs::is_regular_file(status)) {
63-
cerr << configFileName << ": Not a file." << endl;
64-
return -1;
65-
}
66-
67-
if ((perm & fs::perms::owner_read) == fs::perms::none) {
68-
cerr << configFileName << ": No access to read." << endl;
69-
return -1;
70-
}
71-
7259
// Keeps the default values in case the yaml is missing a field
7360
config.readFromFile(configFileName);
7461
} else {

0 commit comments

Comments
 (0)