Permalink
Browse files

foreign imported grammars now have their initialize functions invoked

  • Loading branch information...
1 parent 690f5c3 commit 027129c3cc07b651d2f481ef4e631d6c5d31d92f @bmavity committed Sep 28, 2010
Showing with 167 additions and 7 deletions.
  1. +3 −0 newLineGrammar.ometa
  2. +25 −0 newLineTest.js
  3. +1 −0 ometa-base.js
  4. +1 −1 ometa-node.js
  5. +1 −1 package.json
  6. +136 −5 standalone.html
View
@@ -0,0 +1,3 @@
+ometa T <: Parser {
+ t = '\n'
+}
View
@@ -0,0 +1,25 @@
+var sys = require('sys'),
+ fs = require('fs'),
+ ometa = require(__dirname + '/index.js');
+
+var parse = function(stringToParse, rule) {
+ fs.readFile(__dirname + '/newLineGrammar.ometa', function(err, file) {
+ if(err) console.log(err);
+ ometa.createParser(file.toString(), function(err, parser) {
+ if(err) {
+ sys.puts(err.inner.toString());
+ } else {
+ parser.parse(stringToParse, rule, function(err, result) {
+ if(err) {
+ console.log(err);
+ } else {
+ console.log(result);
+ }
+ });
+ }
+ });
+ });
+};
+
+
+module.exports.parse = parse;
View
@@ -373,6 +373,7 @@ OMeta = {
var g = this._apply("anything"),
r = this._apply("anything"),
gi = objectThatDelegatesTo(g, {input: makeOMInputStreamProxy(this.input)})
+ gi.initialize();
var ans = gi._apply(r)
this.input = gi.input.target
return ans
View
@@ -8,7 +8,7 @@ require("./bs-ometa-js-compiler")
var translateCode = function(s) {
var translationError = function(m, i) { alert("Translation error - please tell Alex about this!"); throw fail },
- tree = BSOMetaJSParser.matchAll(s, "topLevel", undefined, function(m, i) { throw fail.delegated({errorPos: i}) })
+ tree = BSOMetaJSParser.matchAll(s, "topLevel", undefined, function(m, i) { throw objectThatDelegatesTo(fail, {errorPos: i}) })
return BSOMetaJSTranslator.match(tree, "trans", undefined, translationError)
}
View
@@ -6,5 +6,5 @@
, "description": "JavaScript Implementation of OMeta Parsing Language"
, "engines": { "node": ">= 0.2.0" }
, "main": "./index.js"
-, "version": "0.2.0"
+, "version": "0.2.1"
}
View
@@ -1,6 +1,5 @@
<html>
<head>
- <script src="prototype.js"></script>
<script src="lib.js"></script>
<script src="ometa-base.js"></script>
<script src="parser.js"></script>
@@ -11,10 +10,142 @@
<script src="ometa-script-tag.js"></script>
<script type="text/x-ometa-js">
- ometa M <: Parser { ones = digit* }
- console.log(M.matchAll("1", "ones"))
- console.log(M.matchAll("0", "ones"))
- console.log(M.matchAll("111", "ones"))
+ometa CssSelector <: Parser {
+ crChar = '\r',
+ ffChar = '\f',
+ nlChar = '\n',
+ tabChar = '\t',
+ lineEnding = crChar | ffChar | nlChar,
+ tabOrLineEnding = tabChar | lineEnding,
+
+
+ ident = '-' nmstart:s nmchar*:cs -> { '-' + s + cs.join('') }
+ | nmstart:s nmchar*:cs -> { s + cs.join('') },
+ name = nmchar+:n -> { n.join('') },
+ nmstart = ('_' | letter | nonascii | escape):n -> { n },
+ nonascii = '',
+ unicode = '',
+ escape = unicode | '',
+ nmchar = '_' | '-' | letterOrDigit | nonascii | escape,
+ num = digit+: d -> d.join('')
+ | digit* '.' digit+,
+ string = (string1 | string2):s -> { s },
+ //string1 = '\"' (~(lineEnding | '\"') | '\\' nl | nonascii | escape)*:s '\"' -> { '\"' + s.join('') + '\"' },
+ string1 = '"' letter*:s '"' -> { '"' + s.join('') + '"' },
+ string2 = '\'' (~(lineEnding | '\'') | '\\' nl | nonascii | escape)*:s '\'' -> { '\'' + s.join('') + '\'' },
+ //invalid = invalid1 | invalid2,
+ //invalid1 = '\"' (~(lineEnding | '\"') | '\\' nl | nonascii | escape)*,
+ //invalid2 = '\'' (~(lineEnding | '\'') | '\\' nl | nonascii | escape)*,
+ nl = crChar nlChar
+ | lineEnding,
+ D = 'd' | 'D',
+ E = 'e' | 'E',
+ N = 'n' | 'N',
+ O = 'o' | 'O',
+ T = 't' | 'T',
+ V = 'v' | 'V',
+
+
+ S = ' ' -> { ' ' }
+ | '\n' -> { '\n' },
+ INCLUDES = '~' '=' -> { '~=' },
+ DASHMATCH = '|' '=' -> { '|=' },
+ PREFIXMATCH = '^' '=' -> { '^=' },
+ SUFFIXMATCH = '$' '=' -> { '$=' },
+ SUBSTRINGMATCH = '*' '=' -> { '*=' },
+ IDENT = ident,
+ STRING = string:s -> { s },
+ FUNCTION = ident:i '(' -> { i + '(' },
+ NUMBER = num:n -> { n },
+ HASH = '#' name:n -> { '#' + n },
+ PLUS = S+ '+' -> { ' +' }
+ | '+' -> { '+' },
+ // First line of next selector is a Css Hack
+ GREATER = '>' '>' -> { '> >' }
+ | S+ '>' -> { ' >' }
+ | '>' -> { '>' },
+ COMMA = S+ ',' -> { ' ,' }
+ | ',' -> { ',' },
+ TILDE = S+ '~' -> { ' ~' }
+ | '~' -> { '~' },
+ NOT = ':' N O T '(' -> { ':not(' },
+ ATKEYWORD = '@' ident,
+ INVALID = invalid,
+ PERCENTAGE = num:n '%' -> { n + '%' },
+ DIMENSION = num:n ident:i -> { n + i },
+ CDO = '<' '!' '-' '-',
+ CDC = '-' '-' '>',
+
+
+ selectors_group = selector:pre comma_separated_selector*:post -> { self.add(pre + post.join('')); self },
+ comma_separated_selector = COMMA:com S*:spacing selector:sel -> { com + spacing.join('') + sel },
+ selector = simple_selector_sequence:sim (combined_sequence)*:additional -> { sim + additional.join('') }
+ // Css Hack
+ | combined_sequence*:comb -> { comb.join('') },
+ combinator = PLUS:p S+ -> { p + ' ' }
+ | PLUS:p -> { p }
+ | GREATER:g S+ -> { g + ' ' }
+ | GREATER:g -> { g }
+ | TILDE:t S+ -> { t + ' ' }
+ | TILDE:t -> { t }
+ | S+:spacing -> { spacing.join('') },
+ combined_sequence = combinator:comb simple_selector_sequence:sel -> { comb + sel }
+ | combinator+:comb simple_selector_sequence:sel -> { comb.join('') + sel },
+ non_namespaced_selector = (HASH | class | attrib | negation | pseudo):sel -> { sel },
+ simple_selector_sequence = namespace_prefix:pre '*' non_namespaced_selector*:post -> { pre + '*' + post.join('') }
+ | namespace_prefix:pre element_name:ele non_namespaced_selector*:post -> { pre + ele + post.join('') }
+ | '*' non_namespaced_selector*:post -> { '*' + post.join('') }
+ | element_name:ele non_namespaced_selector*:post -> { ele + post.join('') }
+ | non_namespaced_selector+:sels -> { sels.join('') }
+ // Css Hack
+ | expression:ex -> { ex },
+ namespace_prefix = ('*' | IDENT):pre '|' -> { pre + '|' }
+ | '|' -> { '|' },
+ // First line of the next selector is a Css Hack
+ element_name = IDENT:i '*' -> { i + '*' }
+ | IDENT:i -> { i },
+ class = '.' IDENT:i -> { '.' + i },
+
+ attrib = '[' S* possible_namespaced_attrib:att ']' -> { '[' + att + ']' },
+ possible_namespaced_attrib = namespace_prefix:pre ident_with_possible_postfix:post -> { pre + post }
+ | ident_with_possible_postfix:post -> { post },
+ ident_with_possible_postfix = IDENT:left S* attrib_match:match S* (IDENT | STRING):right S* -> { left + match + right }
+ | IDENT:i S* -> { i },
+ attrib_match = (PREFIXMATCH | SUFFIXMATCH | SUBSTRINGMATCH | equals_match | INCLUDES | DASHMATCH):m -> { m },
+ equals_match = '=' -> { '=' },
+ pseudo = ':' ':' (functional_pseudo | IDENT):i -> { '::' + i }
+ | ':' (functional_pseudo | IDENT):i -> { ':' + i },
+ functional_pseudo = FUNCTION:f S* full_expression:e ')' -> { f + e + ')' }
+ // Css Hack for :-moz-any(...)
+ | FUNCTION:f S* selectors_group:sel ')' -> { f + sel + ')' },
+
+ expression_content = (PLUS | '-' | PERCENTAGE | DIMENSION | NUMBER | STRING | IDENT):e -> { e },
+ expression = expression_content:ec S+ expression:e -> { ec + ' ' + e }
+ | expression_content:ec expression:e -> { ec + e }
+ | expression_content:ec S* -> { ec },
+ full_expression = (expression)+:ea -> { ea.join('') },
+ negation = NOT:n S* negation_arg:na S* ')' -> { n + na + ')' },
+
+ //negation_arg = (type_selector | universal | HASH | class | attrib | pseudo):na -> { na }
+ //Technically not allowed, but here for scss compatibility
+ negation_arg = (selectors_group):na -> { na }
+}
+
+CssSelector.initialize = function() {
+ var s;
+
+ this.toString = function() {
+ return s;
+ };
+
+ this.add = function(st) {
+ s = st;
+ };
+};
+
+ console.log(CssSelector.matchAll("E,\nF", "selectors_group").toString());
+ console.log(CssSelector.matchAll("E\nF", "selectors_group").toString());
+ console.log(CssSelector.matchAll("E, F\nG, H", "selectors_group").toString());
</script>
<body>

0 comments on commit 027129c

Please sign in to comment.