Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

false commit. really a backup

got an express server, will generate a "html" page from which to run
test parsers. Most material to get DH type 2 (a rule in red and its
kids in blue). css missing though. With that I should be done with the
lite viewport. Editing will come later
  • Loading branch information...
commit f7bef0a3ef3e0bde388d21b57c75d9bf5ee5b537 1 parent c57b2b6
@cognominal authored
View
5 README.md
@@ -1,6 +1,9 @@
-Tentatively called zbrew
+Tentatively called zbrew.
+Note : having no user yet, I play fast and loose.
+No git branches when I refactorize. I will probably tag somme "interesting"
+versions. This will soon change for the better.
# Goal : viewing code in web pages
View
20 bin/jsonParseTree
@@ -1,4 +1,6 @@
#! /usr/bin/env perl6
+say "this is currently broken pending further refactorization"
+exit;
use JSON::Tiny::Grammar;
use JSON::Tiny;
@@ -47,6 +49,18 @@ interested in.
=end pod
+sub deepToJSON(Match:D $match, $ruleNm = 'TOP') {
+ sub recurse($match, $rulenNm) {
+ my $v = $match.chunks.map: { .value ~~ Str ?? to-json .value !! recurse .value, .key }
+ '{' ~ $ruleNm.Str.perl ~ ":[" ~ $v.join( ',') ~ ']}';
+ # to-json do { Pair.new: key => $ruleNm.Str.perl, value => $v };
+ }
+ my $parseTree = recurse $match, $ruleNm;
+ qq|\{"rules":{to-json %ruleNms}, :$parseTree, "docco":{to-json %docco}\}|;
+}
+
+
+
sub shallowToJSON(Match:D $match, $ruleNm = 'TOP') {
my @tokens;
sub recurse($match, $ruleNm) {
@@ -61,12 +75,6 @@ sub shallowToJSON(Match:D $match, $ruleNm = 'TOP') {
-sub deepToJSON(Match:D $match, $ruleNm = 'TOP') {
- my $v = $match.chunks.map: { .value ~~ Str ?? to-json .value !! deepToJSON .value, .key }
- '{' ~ $ruleNm.Str.perl ~ ":[" ~ $v.join( ',') ~ ']}';
-# to-json do { Pair.new: key => $ruleNm.Str.perl, value => $v };
-}
-
=begin END
sub ruleNAmes(Match:D $match) {
View
71 p6lib/Match/JSON.pm
@@ -0,0 +1,71 @@
+use JSON::Tiny;
+module Match::JSON;
+
+=begin pod
+
+The .perl representation of a Match is unacceptably verbose.
+This routine proposes a terser match serialization in json format.
+We may be interested only in the tokens or the whole parse tree,
+depending on which we call either &shallowToJSON or &deepToJSON
+
+for &shallowToJSON, the keys of %*leafs are the token list type we are
+interested in.
+
+:%gram associates rule names to their representation.
+It allows the lite viewport to show the current rule
+
+:%doc associates rules names with their docco highlighting. It has a default
+which may help if the grammar has sensible rule names.
+
+Finally :$ruleNm should be the top grammar rule name.
+
+=end pod
+
+
+
+# TODO : should pass as a parameter
+# translate rule name into CSS name.
+my %docco_ = {
+ TOP => 'TOP',
+ string => 's',
+ number => 'm'
+};
+
+
+=begin pod
+
+&extractRules is a hack. Pass it the source code of a Perl 6 grammar and it
+returns %rules with rule names as keys and their source code as value.
+
+Note that inner rules are ignored. Example, below $<b> defines a inner rule.
+With that example &extractRules returns C<{ ab -> 'token ab { a $<b>=b } }>
+
+Grammar AB {
+ token ab { a $<b>=b }
+}
+
+=end pod
+
+sub extractRules($_) is export {
+ my %gram;
+ while m:s:c/[rule|regex|token] $<nm>=\w+ [ \V* | .*? ^^ ] '}' / {
+ %gram{$<nm>} = ~$/; }
+ %gram;
+}
+
+sub deepToJSON (Match:D $match, :%rules, :%docco = %docco_,
+ :$ruleNm = 'TOP') is export {
+ sub recurse($match, $rulenNm) {
+ my $v = $match.chunks.map: { .value ~~ Str ?? to-json .value !! recurse .value, .key };
+ '{' ~ $ruleNm.Str.perl ~ ":[" ~ $v.join( ',') ~ ']}';
+# previous line preferred to next to avoid extra spaces.
+# to-json do { Pair.new: key => $ruleNm.Str.perl, value => $v };
+ }
+ my $parseTree := recurse $match, :$ruleNm;
+ my $jsonRules := to-json %rules;
+
+# I would like de write :$parseTree, need a C<qq :j ||> and $str is JSON;
+ qq|\{"rules":$jsonRules, "parseTree":$parseTree, "docco":{to-json %docco}\}|;
+
+}
+
View
7 server/bin/server
@@ -0,0 +1,7 @@
+express = require 'express'
+app = express()
+
+app.get '/', (req, res) ->
+ res.send 'Hello World'
+
+app.listen 8080
View
35 server/bin/server.coffee
@@ -0,0 +1,35 @@
+# quick and dirty web server on port 8080 to test highlighting
+express = require 'express'
+{ spawn } = require 'child_process'
+process.chdir 'server/data'
+log = console.log
+log "ok"
+
+ls = spawn 'ls'
+fileNms = []
+
+ls.stdout.on 'data', (data) ->
+ s = data.toString()
+ fileNms = s.split "\n";
+ fileNms.pop()
+ log fileNms
+
+
+app = express()
+
+html = """
+ <body>
+ testing <a href="bin/hilite">hilighighting</a>
+ </body>
+"""
+
+app.get '/', (req, res) ->
+ res.writeHead 200, {'Content-Type': 'text/html'}
+ res.end html
+
+
+
+app.get '/:id', (req, res) ->
+ res.send req.params.id
+
+app.listen 8080
View
0  server/data/another
No changes.
View
21 server/data/bin/hilite.p6
@@ -0,0 +1,21 @@
+use JSON::Tiny;
+use lib './p6lib';
+use Match::JSON;
+
+# the goal of this moronic grammar is to demonstrate how DH
+# shows how code is parsed. Here, it visibly demonstrate the
+# effect of relative operator precedence.
+grammar G {
+ token mul { <term> '*' <term> }
+ token TOP { <term> '+' <term> }
+ token term { <int> || <id> }
+ token int { \d+ }
+ token id { \w+ }
+};
+
+my $match = G.parse: "a+b*c";
+die unless $match;
+say $match.perl;
+my $fnm = %*ENV<HOME> ~ '/git/codeview/server/data/bin/hilite.p6';
+my %rules = extractRules slurp $fnm;
+say deepToJSON $match, :%rules ;
View
1  server/data/placeholder
@@ -0,0 +1 @@
+placeholder
View
7 t
@@ -0,0 +1,7 @@
+grammar G {
+ token mul { <term> '*' <term> }
+ token add { <term> '+' <term> }
+ token term { <int> || <id> }
+ token int { \d+ }
+ toekn id { \w+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.