ã¿ã¤ãã«ã®éãã§ããJavaScriptã«é¢ãã¦ã¯å¤å°ã¯ç¥ã£ã¦ãã¾ãããç¥èãå ¨ç¶ã¢ãããã¼ãããã¦ããªãã®ã§ããã®æ©ä¼ã«åå ¥éãã¦ã¿ããã¨ã«ãã¾ããã以ä¸ã¯package.jsonãªããBabelé¢é£ã§è©¦è¡é¯èª¤ããã®ã§ãçµæ§å¤ãªæ§æã«ãªã£ã¦ããããã
{ "name": "es_copmbinator", "version": "0.0.1", "description": "A ES2015 Parser Combinator Library", "main": "index.js", "scripts": { "test": "ava -v" }, "repository": { "type": "git", "url": "git+https://github.com/kmizu/es_copmbinator.git" }, "keywords": [ "parser" ], "author": "Kota Mizushima", "license": "MIT", "bugs": { "url": "https://github.com/kmizu/es_copmbinator/issues" }, "homepage": "https://github.com/kmizu/es_copmbinator#readme", "devDependencies": { "ava": "^0.15.1", "babel-preset-es2015": "^6.9.0", "babelify": "^7.3.0" } }
以ä¸ãééããåé¡ãES2015ã®æ©è½ã«ã¤ãã¦ã
ã¢ãã¼é¢æ°
ç¡åé¢æ°ã®ç°¡ç¥è¨æ³ï¼ã¨ããã ãã§ã¯ãªããï¼ãæ¬ä½ãå¼ã§ããå ´åãreturnãçç¥ã§ããããããã¨ãããããããã¯ã¯å¼ã§ã¯ãªãã¨ããã®ãæ£ãããããªãã¨ãä¸éå端ãªä»æ§ã ããããã¾ã§ã®JavaScriptã®ä»æ§ã¨ä¸è²«æ§ãä¿ã¤ããã«ã¯ä»æ¹ãªãã®ãã
const add = (x, y) =>(x, y) const printAdd = (x, y) => { util.log(x); util.log(y); return x + y; // returnå¿ é };
ã¯ã©ã¹å®ç¾©
å²ã¨æ®éã«ã¯ã©ã¹ãå®ç¾©ã§ãããæé»ã®super();
å¼ã³åºãããªããã¨ãç¥ããªãã£ããããã¡ãã£ã¨ããããããã¨ã©ã¼ã¡ãã»ã¼ã¸ãåºãã¦ã»ããããã ãã¡ã½ããã®å®ç¾©ããã¡ã½ããå(å¼æ°...) { ... }
ã ãã§æ¸ããã®ã¯ä¾¿å©ã
class ParseResult { constructor(next) { this.next = next; } } class ParseSuccess extends ParseResult { constructor(value, next) { super(next); this.value = value; } isSuccess() { return true; } } class ParseFailure extends ParseResult { constructor(next) { super(next); } isSuccess() { return false; } }
letã¨const
å
±ã«ãããã¯ã¹ã³ã¼ããlet
ã¯å¤æ´å¯è½ãconst
ã¯å¤æ´ä¸è½ãã¨ãããããã»ã¨ãã©const
ãã使ããªãã£ãã
ã¡ã½ããã ããåãåºããªãï¼
以ä¸ã®ãããªãã¨ããããã£ã
const combinator = new ParserCombinator; const or = combinator.or; or(new StringParser("FOO"), new StringParser("BAR"));
ã ããthis
ããªãã¨æããã¦ãã¾ããããã¯ã¾ããããã¾ã§ã®JavaScriptã®ä»æ§ãèããã¨ç´å¾ã§ããããæ§æçã«ã¯this
ãæç¸ããã¦ãã¦æ¬²ããã®ã§æ®å¿µã
trueã¨å¤æ°åã¨ã¡ã½ããåã¨
const true = 1;
ã¨ããã¨æãããã®ã«ã
class Hoge { true() { } } const hoge = new Hoge(); hoge.true();
ã¯æãããªãã
ã«ãå®éã«ããäºç´èªã¯èå¥åã®ã¿ã«é©ç¨ããã¾ããï¼ä»¥ä¸ç¥ï¼ã
a.import a["import"] a = { import: "test" }
ã¨ãã£ã¦ãããããä»æ§ã§ãããã¨ããããã
ã¯ã©ã¹ä¸ã®æé»ã®thisï¼thisã®çç¥ï¼ããªã
ããã¾ããçµå±ãããã¾ã§ã®JavaScriptãèããã¨ä»æ¹ç¡ãã®ã ãããã©ã
class OrParser extends Parser { constructor(lhs, rhs) { super(); this.lhs = lhs; this.rhs = rhs; } parse(input) { const r1 = this.lhs.parse(input); if(r1.isSuccess()) { return r1; }else{ return this.rhs.parse(input); } } }
ã¯
class OrParser extends Parser { constructor(lhs, rhs) { super(); this.lhs = lhs; this.rhs = rhs; } parse(input) { const r1 = lhs.parse(input); if(r1.isSuccess()) { return r1; }else{ return rhs.parse(input); } } }
ã¨æ¸ãããã£ãã
å ¨ä½çã«ãES2015ã®è¨èªèªä½ã§ãä»ã®ã¨ããããã»ã©ããã£ãã¨ã¯æããªãã£ãã¨ããããã©ã£ã¡ãã¨ããã¨å¨è¾ºç°å¢ã®æ´åã§ããã£ãã
ã¦ãªæãã§çã¾ããã®ããã¡ãã
以åã«ããã®ã¨ã³ããªã§ãæ°ããè¨èªãè¦ããããã«ã¯ã¾ããã¼ã¶ã³ã³ããã¼ã¿ã©ã¤ãã©ãªãä½æããã¨æ¸ãã¦ããããã¾ãã«ãã®ã¾ãã¾ã¨ãããããã¼ã¶ã³ã³ããã¼ã¿ã¯ãè¨èªæ©è½ãå¤å°ã¯ä½¿ããªãã¨ä½ããªãé¡ã®ãã®ãªã®ã§ããã¯ã"Hello, World!"ã®ä»£ããã¨ãã¦é©ãã¦ããã¨ã¾ãã¾ã確信ãã次第ã§ãã
ã¨ããã§ããã¹ãä¸ã«æ¸ãããååæ¼ç®å¼ã®ãã¼ã¶ãªã®ã§ããããã£ã¨åé·ã«ãªããã¨æãããã
const E = () => A(); const A = () => c.f(() => M()).chainl( ( c.s("+").map((op) => (lhs, rhs) => lhs + rhs) ).or( c.s("-").map((op) => (lhs, rhs) => lhs - rhs) ) ); const M = () => c.f(() => P()).chainl( ( c.s("*").map((op) => (lhs, rhs) => lhs * rhs)).or( c.s("/").map((op) => (lhs, rhs) => lhs / rhs) ) ); const P = () => (c.s("(").cat(c.f(() => E())).cat(c.s(")"))).map((values) => { return values[0][1]; }).or(c.f(() => N())); const N = () => c.r("[0-9]+").map((n) => parseInt(n));
ããã ãã§æ¸ãã¦ãã¾ã£ãã®ãçµæ§æå¤ã§ããï¼ã¡ããã¨æ°å¤ã®è¨ç®ããã¦ããµã³ãã«ï¼ãããã¯ãã¢ãã¼é¢æ°ã®æ¬ä½ããå¼ã®å ´åã¯return
ãçç¥ã§ããã®ã¨ãchainl
ããã¯ãå¼·åãªã³ã³ããã¼ã¿ã§ãã£ã¦ããããå®è£
ããã¨ããã¼ã¶ãå®è£
ããã®ãæ ¼æ®µã«æ¥½ã«ãªãã¨ãããã¨ããªã¨æãã¾ãããã ãè¦åã«ç¸å½ãããã®ãå
¨ã¦é¢æ°ã§ã©ããããã¦ããéããé
延è©ä¾¡ããããã£ã¼ã«ãã¨ãæ¸ããªãã®ã§ãè¥å¹²ä¸æ ¼å¥½ã§ããã¾ãä»æ¹ãªãã
ã¨ã¾ããããªæãã§ãããES2015ã¯åå¿è ãããã¨ããã ãããã以åã®JavaScriptããã¾ãå¾æã§ã¯ãªãã®ã§ãããã³ãã©ãããããããããã¾ããããããã¯ããã³ãã§ããã ããã¨ãããããã§ãã