From 5252724ab2534b7bf6baee24bd02dd8a4761e2ac Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 22 Dec 2016 07:55:06 +0300 Subject: [PATCH] minor cleanup on macro comment handling... Signed-off-by: Alex A. Naanou --- Slang/slang.js | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/Slang/slang.js b/Slang/slang.js index b8b0de2..4961211 100755 --- a/Slang/slang.js +++ b/Slang/slang.js @@ -59,15 +59,21 @@ function run(context){ // XXX make this add '\n' / EOL words to the stream... //var SPLITTER = /\s*\([^\)]*\)\s*|\s*--.*[\n$]|\s*"([^"]*)"\s*|\s*'([^']*)'\s*|\s+/m var SPLITTER = RegExp([ + /* XXX there are two ways to deal with comments: + // 1) lexer-based -- this section commented, next uncommented... + // 2) macro-based -- this section uncommented, next commented... + // #2 is a bit buggy... // terms to keep in the stream... - /*'('+[ + '\\s*('+[ '\\n', '--', - ].join('|')+')',*/ + ].join('|')+')', + //*/ - // comments... + //* lexer comments... '\\s*\\([^\\)]*\\)\\s*', '\\s*--.*[\\n$]', + //*/ // quoted strings... '\\s*"([^"]*)"\\s*', @@ -82,6 +88,27 @@ var SPLITTER = RegExp([ 'm') +// helpers... +// XXX should these skip quoted ends? +var collectUntil = function(end){ + return function(context){ + var res = ['--'] + var code = context.code + var cur = code.shift() + res.push(cur) + while(cur != end && code.length > 0){ + cur = code.splice(0, 1)[0] + res.push(cur) + } + return res + } +} +var dropUntil = function(end){ + var collector = collectUntil(end) + return function(context){ collector(context) } +} + + // pre-processor namespace... var PRE_NAMESPACE = { // comment... @@ -90,17 +117,8 @@ var PRE_NAMESPACE = { // drop everything until '\n' // // NOTE: this depends on explicit '\n' words... - '--': function(context){ - var res = ['--'] - var code = context.code - var cur = code.splice(0, 1)[0] - res.push(cur) - while(cur != '\n' && code.length > 0){ - cur = code.splice(0, 1)[0] - res.push(cur) - } - console.log(res.join(' ')) - }, + '--': dropUntil('\n'), + //'(': dropUntil(')'), // XXX use the real reader... // block... @@ -154,7 +172,7 @@ var PRE_NAMESPACE = { }, // a no op... - '\n': function(){ console.log('NL') }, + '\n': function(){ }, }