Browse Source

website online/try: serve different example

master
Futago-za Ryuu 3 years ago
parent
commit
9dc4d4a5ef
3 changed files with 36 additions and 30 deletions
  1. +5
    -0
      server.js
  2. +30
    -0
      website/vendor/pegjs/arithmetics.pegjs
  3. +1
    -30
      website/views/online.ejs

+ 5
- 0
server.js View File

@ -4,6 +4,7 @@ const bodyParser = require( "body-parser" );
const express = require( "express" );
const layout = require( "express-layout" );
const logger = require( "morgan" );
const { readFileSync } = require( "fs" );
const { join } = require( "path" );
const ms = require( "pretty-ms" );
const rollup = require( "rollup" );
@ -58,6 +59,7 @@ app.get( "/", ( req, res ) => {
} );
const LEGACY_EXAMPLE = readFileSync( path( "website", "vendor", "pegjs", "arithmetics.pegjs" ), "utf8" );
app.get( "/online", ( req, res ) => {
res.render( "online", {
@ -65,6 +67,7 @@ app.get( "/online", ( req, res ) => {
title: "Online version",
layout: "layout-online",
pegjs: "/vendor/pegjs/peg.js",
example: LEGACY_EXAMPLE,
} );
@ -90,6 +93,7 @@ app.get( "/development", ( req, res ) => {
} );
const DEV_EXAMPLE = readFileSync( path( "examples", "arithmetics.pegjs" ) );
app.get( "/development/try", ( req, res ) => {
res.render( "online", {
@ -97,6 +101,7 @@ app.get( "/development/try", ( req, res ) => {
title: "Online Development version",
layout: "layout-online",
pegjs: "/js/peg-bundle.js",
example: DEV_EXAMPLE,
} );


+ 30
- 0
website/vendor/pegjs/arithmetics.pegjs View File

@ -0,0 +1,30 @@
// Simple Arithmetics Grammar
// ==========================
//
// Accepts expressions like "2 * (3 + 4)" and computes their value.
Expression
= head:Term tail:(_ ("+" / "-") _ Term)* {
return tail.reduce(function(result, element) {
if (element[1] === "+") { return result + element[3]; }
if (element[1] === "-") { return result - element[3]; }
}, head);
}
Term
= head:Factor tail:(_ ("*" / "/") _ Factor)* {
return tail.reduce(function(result, element) {
if (element[1] === "*") { return result * element[3]; }
if (element[1] === "/") { return result / element[3]; }
}, head);
}
Factor
= "(" _ expr:Expression _ ")" { return expr; }
/ Integer
Integer "integer"
= _ [0-9]+ { return parseInt(text(), 10); }
_ "whitespace"
= [ \t\n\r]*

+ 1
- 30
website/views/online.ejs View File

@ -14,36 +14,7 @@
<td>
<div class="textarea-wrapper">
<textarea class="code" id="grammar" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" disabled>
// Simple Arithmetics Grammar
// ==========================
//
// Accepts expressions like "2 * (3 + 4)" and computes their value.
Expression
= head:Term tail:(_ ("+" / "-") _ Term)* {
return tail.reduce(function(result, element) {
if (element[1] === "+") { return result + element[3]; }
if (element[1] === "-") { return result - element[3]; }
}, head);
}
Term
= head:Factor tail:(_ ("*" / "/") _ Factor)* {
return tail.reduce(function(result, element) {
if (element[1] === "*") { return result * element[3]; }
if (element[1] === "/") { return result / element[3]; }
}, head);
}
Factor
= "(" _ expr:Expression _ ")" { return expr; }
/ Integer
Integer "integer"
= _ [0-9]+ { return parseInt(text(), 10); }
_ "whitespace"
= [ \t\n\r]*
<%- example %>
</textarea>
</div>
</td>


Loading…
Cancel
Save