Skip to content
This repository was archived by the owner on Jun 5, 2020. It is now read-only.

Commit 71a7736

Browse files
committed
Proper indentation style
1 parent 0c645a1 commit 71a7736

6 files changed

Lines changed: 65 additions & 35 deletions

File tree

MetaScript.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@
5353
MetaScript.compile = function(source) {
5454
source = source+"";
5555

56-
var index = 0, // Current working index
57-
expr = /(\/\/\?|\/\*\?)/g, // Line/block expression
58-
exprLine = /\n|$/g, // Line terminator
59-
exprBlock = /\*\//g, // Block terminator
60-
match, matchEnd, // Matches
61-
s, // Temporary string
62-
indent = '', // Indentation
63-
lastIndent = '', // Last indentation
64-
out = []; // Output stack
56+
var index = 0, // Current working index
57+
expr = /(\/\/\?|\/\*\?)(=?)/g, // Line/block expression
58+
exprLine = /\n|$/g, // Line terminator
59+
exprBlock = /\*\//g, // Block terminator
60+
match, matchEnd, // Matches
61+
s, // Temporary string
62+
indent = '', // Indentation
63+
lastIndent = '', // Last indentation
64+
out = []; // Output stack
6565

6666
// Escapes a string to be used in a JavaScript string enclosed in single quotes
6767
function escape(s) {
@@ -88,7 +88,7 @@
8888
match;
8989
while (match = expr.exec(source)) {
9090
s = source.substring(index, match.index+1);
91-
if (s !== '') out.push(' write(\''+escape(s)+'\');\n');
91+
out.push(' write(\''+escape(s)+'\');\n');
9292
index = match.index+1;
9393
}
9494
s = source.substring(index, source.length);
@@ -102,11 +102,12 @@
102102
s = source.substring(index, match.index);
103103

104104
// Look if it is a line or a block of meta
105-
if (match[1] === '/'+'/?') { // Line
106-
107-
// Trim whitespaces in front of the line and remember the indentation for include() and such
108-
s = s.replace(/\n([ \t]*)$/, function($0, $1) { indent = $1; return '\n'; });
105+
if (match[1].indexOf('*') < 0) { // Line
109106

107+
// Trim whitespaces in front of the line and remember the indentation
108+
if (match[2] !== '=')
109+
s = s.replace(/(^|\n)([ \t]*)$/, function($0, $1, $2) { indent = $2; return $1; });
110+
110111
// Append leading contents
111112
append(s);
112113

@@ -119,14 +120,17 @@
119120
out.push('__=\''+escape(lastIndent = indent)+'\';\n');
120121
}
121122
out.push(evaluate(source.substring(match.index+3, matchEnd.index).trim()));
122-
123+
if (match[2] === '=')
124+
out.push('writeln();\n');
125+
123126
// Move on
124127
index = matchEnd.index+1;
125128

126129
} else { // Block
127130

128-
// Trim whitespaces in front of the block if it is using a dedicated line and remember the indentation
129-
s = s.replace(/\n([ \t]+)$/, function($0, $1) { indent = $1; return '\n'; });
131+
// Trim whitespaces in front of the block and remember the indentation
132+
if (match[2] !== '=')
133+
s = s.replace(/(^|\n)([ \t]*)$/, function($0, $1, $2) { indent = $2; return $1; });
130134

131135
// Append leading contents
132136
append(s);
@@ -181,7 +185,7 @@
181185
///////////////////////////////////////////// Built-in functions ///////////////////////////////////////////////
182186

183187
/**
184-
* Writes some contents to the document.
188+
* Writes some contents to the document (no indentation).
185189
* @param {*} s Contents to write
186190
*/
187191
function write(s) {
@@ -193,7 +197,8 @@
193197
* @param {*} s Contents to write
194198
*/
195199
function writeln(s) {
196-
out.push(s+"\n");
200+
if (typeof s === 'undefined') s = '';
201+
out.push(s + "\n");
197202
}
198203

199204
/**
@@ -237,9 +242,10 @@
237242
while (indent_str.length < indent) indent_str += ' ';
238243
indent = indent_str;
239244
}
240-
var lines = str.split("\n");
245+
var lines = str.split(/\n/);
241246
for (var i=0; i<lines.length; i++) {
242-
lines[i] = indent + lines[i];
247+
if (lines[i].trim() !== '')
248+
lines[i] = indent + lines[i];
243249
}
244250
return lines.join("\n");
245251
}

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,19 @@ function writeInt8(value, offset) {
8888
}
8989
```
9090

91+
More example will soon be [available in the wiki](https://github.com/dcodeIO/MetaScript/wiki).
92+
9193
API
9294
---
9395
The API is pretty much straight forward:
9496

95-
* **new MetaScript(source:string)** creates a new instance with `source` compiled to a meta program
96-
* **MetaScript#program** contains the meta program's source
97+
* **new MetaScript(source:string)** creates a new instance with `source` compiled to a meta program.
98+
* **MetaScript#program** contains the meta program's source.
9799
* **MetaScript#transform(scope:Object, basedir:string=):string** runs the meta program, transforming the source
98100
depending on what's defined in `scope` and returns the final source. `basedir` specifies the base directory for top
99-
level relative includes and defaults to `.` under node.js and `/` in the browser
100-
* **MetaScript.compile(source:string):string** compiles the source to a meta program and returns its JavaScript source
101+
level relative includes and defaults to `.` under node.js and `/` in the browser.
102+
* **MetaScript.compile(source:string):string** is just the raw compiler that compiles the source to a meta program and
103+
returns its JavaScript source.
101104

102105
Command line
103106
------------
@@ -109,8 +112,7 @@ Transforming sources on the fly is simple with node:
109112
Usage: metascript sourcefile [basedir] -SOMEDEFINE="some" -OTHERDEFINE="thing" [> outfile]
110113
```
111114

112-
And in the case that you have to craft your own runtime, the MetaScript compiler that generates raw meta programs is
113-
also available as `metac`:
115+
And in the case that you have to craft your own runtime, the raw compiler is also available as `metac`:
114116

115117
```
116118
Usage: metac sourcefile [> outfile]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "metascript",
3-
"version": "0.9.2",
3+
"version": "0.9.3",
44
"author": "Daniel Wirtz <dcode@dcode.io>",
55
"description": "Meta programming in JavaScript, e.g. to build different versions of a library.",
66
"main": "./MetaScript.js",

tests/someinclude.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
console.log(/*?== "included" */);
22

3+
// This will be indented:
34
//? if (YEP) include("sub/someotherinclude.js");

tests/somemeta.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
//? if (typeof WHAT === 'undefined') var WHAT = define('WHAT', false);
2+
//? if (typeof VERSION === 'undefined') var VERSION = define('VERSION', '1.0');
3+
4+
MyLibrary.VERSION = /*?== VERSION */;
5+
// or, alternatively, if VERSION is always string-safe:
6+
MyLibrary.VERSION = "/*?= VERSION */";
7+
// or, alternatively, if you don't mind a missing trailing semicolon:
8+
MyLibrary.VERSION = //?== VERSION
9+
// or, alternatively, if you like it procedural:
10+
MyLibrary.VERSION = /*? write(JSON.stringify(VERSION)) */;
11+
212
//? if (WHAT) {
3-
console.log("it's true");
13+
console.log("WHAT's true");
414
//? } else {
5-
console.log("it's false");
15+
console.log("WHAT's false");
616
//? }
717

818
/*? if (WHAT) */
9-
console.log("it's true");
19+
console.log("WHAT's true");
1020
/*? else */
11-
console.log("it's false");
21+
console.log("WHAT's false");
1222

13-
console.log(/*? if (WHAT) { */ "it's true"+ /*? } else { */ "it's false" /*? } */);
23+
console.log(/*? if (WHAT) { */"WHAT's true"+/*? } else { */"WHAT's false"+/*? } */"");
1424

25+
//? // This is a macro:
1526
//? function assertOffset(varname) {
1627
if (/*?= varname */ < 0 || /*?= varname */ > this.capacity()) {
1728
throw(new RangeError("Illegal /*?= varname */"));
1829
}
1930
//? }
2031
function writeInt8(value, offset) {
32+
// Here the macro is used:
2133
//? assertOffset('offset');
2234
// ...
2335
}
2436

37+
// This will be indented:
38+
//?= 'var j=0;'
39+
// Just like this:
40+
//? write(indent('var j=0;\n', 4));
41+
// But this will not:
42+
//? write('var k=0;\n');
43+
// Got it?
44+
2545
//? var YEP = define("YEP", true);
26-
//? if(YEP) include("someinclude.js")
46+
// This will be indented:
47+
//? if(YEP) include("someinclude.js")
2748

2849
console.log(/*?== "that's it" */);

tests/sub/someotherinclude.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
console.log(/*?== "also included and indented" */);
1+
console.log(/*?== "also included" */);

0 commit comments

Comments
 (0)