Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(527)

Unified Diff: utils/css/tree.dart

Issue 8498020: Beginning of CSS parser using frog parsering infrastructure. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Incorporated Jim's CR Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: utils/css/tree.dart
diff --git a/utils/css/tree.dart b/utils/css/tree.dart
new file mode 100644
index 0000000000000000000000000000000000000000..0a0dbed3278dbfc87aee492792cc6b504db8d377
--- /dev/null
+++ b/utils/css/tree.dart
@@ -0,0 +1,274 @@
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+// Generated by scripts/tree_gen.py.
+
+/////////////////////////////////////////////////////////////////////////
+// CSS specific types:
+/////////////////////////////////////////////////////////////////////////
+
+
+class Identifier extends lang.Node {
jimhug 2011/11/10 17:58:56 I think this is really cool - sharing the same bas
+ String name;
+
+ Identifier(this.name, lang.SourceSpan span): super(span) {}
+
+ visit(TreeVisitor visitor) => visitor.visitIdentifier(this);
+
+ String toString() => name;
+}
+
+class SelectorGroup extends lang.Node {
+ // List of SimpleSelector(s) list contain any mix SimpleSelector or
+ // SimpleSlectorName (or class derived from SimpleSelectorName e.g.,
+ // IdSelector, ClassSelector, ElementSelector, PseudoClassSelector,
+ // PseudoElementSelector, NotSelector, or Attribute
+ List<lang.Node> selectors;
+
+ SelectorGroup(this.selectors, lang.SourceSpan span): super(span) {}
+
+ visit(TreeVisitor visitor) => visitor.visitSelectorGroup(this);
+
+ String toString() {
+ StringBuffer buff = new StringBuffer();
+ for (selector in selectors) {
+ buff.add(selector.toString());
+ buff.add(' ');
+ }
+ return buff.toString();
+ }
+
+ /** A multiline string showing the node and its children. */
+ String toDebugString() {
+ var to = new lang.TreeOutput();
+ var tp = new TreePrinter(to);
+ this.visit(tp);
+ return to.buf.toString();
+ }
+}
+
+/* All other selectors (element, #id, .class, attribute, pseudo, negation,
+ * namespace, *) are derived from this selector.
+ */
+class SimpleSelector extends lang.Node {
+ int _combinator; // +, >, ~ or NONE (space)
+
+ // name is of type IdSelector, ClassSelector, ElementSelector,
+ // PseudoClassSelector, PseudoElementSelector, NotSelector, or Attribute
+ var _name;
+
+ SimpleSelector(this._name, [this._combinator = TokenKind.COMBINATOR_NONE]);
+
+ // Wildcard can be a String just return _name otherwise if identifier we'll
+ // drill into the Identifier and return it's name.
+ String get name() => _name is Identifier ? _name.name : _name;
+
+ bool isCombinatorNone() => _combinator == TokenKind.COMBINATOR_NONE;
+ bool isCombinatorPlus() => _combinator == TokenKind.COMBINATOR_PLUS;
+ bool isCombinatorGreater() => _combinator == TokenKind.COMBINATOR_GREATER;
+ bool isCombinatorTilde() => _combinator == TokenKind.COMBINATOR_TILDE;
+
+ visit(TreeVisitor visitor) => visitor.visitSimpleSelectorName(this);
+
+ String toString() => name;
+}
+
+// element name
+class ElementSelector extends SimpleSelector {
+ ElementSelector(String name,
+ [int combinator = TokenKind.COMBINATOR_NONE]) :
+ super(name, combinator);
+
+ bool isWildcard() => name == '*';
+
+ visit(TreeVisitor visitor) => visitor.visitElementSelector(this);
+
+ String toString() => "$name";
+
+ /** A multiline string showing the node and its children. */
+ String toDebugString() {
+ var to = new lang.TreeOutput();
+ var tp = new TreePrinter(to);
+ this.visit(tp);
+ return to.buf.toString();
+ }
+}
+
+// namespace|element
+class NamespaceSelector extends SimpleSelector {
+ var _namespace; // null, '*' or identifier name
+
+ NamespaceSelector(this._namespace, var name,
+ [int combinator = TokenKind.COMBINATOR_NONE]) :
+ super(name, combinator);
+
+ String get namespace() => _namespace is Identifier ?
+ _namespace.name : _namespace;
+
+ bool isWildcardElement() => _name.isWildcard();
+
+ bool isNamespaceWildcard() => _namespace == '*';
+
+ SimpleSelector get nameAsElementSelector() => _name;
+
+ visit(TreeVisitor visitor) => visitor.visitNamespaceSelector(this);
+
+ String toString() => "$namespace|$name";
+}
+
+// #id
+class IdSelector extends SimpleSelector {
+ IdSelector(String name, [int combinator = TokenKind.COMBINATOR_NONE]) :
+ super(name, combinator);
+
+ visit(TreeVisitor visitor) => visitor.visitIdSelector(this);
+
+ String toString() => "#$name";
+}
+
+// .class
+class ClassSelector extends SimpleSelector {
+ ClassSelector(String name,
+ [int combinator = TokenKind.COMBINATOR_NONE]) : super(name, combinator);
+
+ visit(TreeVisitor visitor) => visitor.visitClassSelector(this);
+
+ String toString() => ".$name";
+}
+
+// :pseudoClass
+class PseudoClassSelector extends SimpleSelector {
+ PseudoClassSelector(String name,
+ [int combinator = TokenKind.COMBINATOR_NONE]) :
+ super(name, combinator);
+
+ visit(TreeVisitor visitor) => visitor.visitPseudoClassSelector(this);
+
+ String toString() => ":$name";
+}
+
+// ::pseudoElement
+class PseudoElementSelector extends SimpleSelector {
+ PseudoElementSelector(String name,
+ [int combinator = TokenKind.COMBINATOR_NONE]) :
+ super(name, combinator);
+
+ visit(TreeVisitor visitor) => visitor.visitPseudoElementSelector(this);
+
+ String toString() => "::$name";
+}
+
+// TODO(terry): Implement
+// NOT
+class NotSelector extends SimpleSelector {
+ NotSelector(String name,
+ [lang.Token combinator = TokenKind.COMBINATOR_NONE]) :
+ super(name, combinator);
+
+ visit(TreeVisitor visitor) => visitor.visitNotSelector(this);
+}
+
+// TODO(terry): Implement
+// [attribute]
+class Attribute extends lang.Node {
+ var name; // NamespaceSelector or SimpleSelector
+ int matchType; // ~=, |=, ^=, $=, *=, =
+ String value;
+}
+
+interface TreeVisitor {
+ void visitSelectorGroup(SelectorGroup node);
+ void visitSimpleSelector(SimpleSelector node);
+ void visitElementSelector(ElementSelector node);
+ void visitNamespaceSelector(NamespaceSelector node);
+ void visitIdSelector(IdSelector node);
+ void visitClassSelector(ClassSelector node);
+ void visitPseudoClassSelector(PseudoClassSelector node);
+ void visitPseudoElementSelector(PseudoElementSelector node);
+ void visitNotSelector(NotSelector node);
+
+ void visitIdentifier(Identifier node);
+
+ // TODO(terry): Defined for ../tree.dart.
+ void visitTypeReference(lang.TypeReference node);
+}
+
+class TreePrinter implements TreeVisitor {
+ var output;
+ TreePrinter(this.output) { output.printer = this; }
+
+ void visitSelectorGroup(SelectorGroup node) {
+ output.heading('Selector Group', node.span);
+ output.writeNodeList('selectors', node.selectors);
+ output.writeln('');
+ }
+
+ void visitSimpleSelector(SimpleSelector node) {
+ if (node.isCombinatorNone()) {
+ output.writeValue('combinator', "NONE");
+ } else if (node.isCombinatorPlus()) {
+ output.writeValue('combinator', "+");
+ } else if (node.isCombinatorGreater()) {
+ output.writeValue('combinator', ">");
+ } else if (node.isCombinatorTilde()) {
+ output.writeValue('combinator', "~");
+ } else {
+ output.writeValue('combinator', "ERROR UNKNOWN");
+ }
+ }
+
+ void visitNamespaceSelector(NamespaceSelector node) {
+ output.heading('Namespace Selector', node.span);
+ visitSimpleSelector(node);
+ output.writeValue('namespace', node._namespace);
+ output.writeNode('name', node._name);
+ }
+
+ void visitElementSelector(ElementSelector node) {
+ output.heading('Element Selector', node.span);
+ visitSimpleSelector(node);
+ if (node.isWildcard()) {
+ output.writeValue('name', '*');
+ } else {
+ output.writeValue('name', node.name);
+ }
+ }
+
+ void visitIdSelector(IdSelector node) {
+ output.heading('Id Selector', node.span);
+ visitSimpleSelector(node);
+ output.writeNode('name', node._name);
+ }
+
+ void visitClassSelector(ClassSelector node) {
+ output.heading('Class Selector', node.span);
+ visitSimpleSelector(node);
+ output.writeNode('name', node._name);
+ }
+
+ void visitPseudoClassSelector(PseudoClassSelector node) {
+ output.heading('Pseudo Class Selector', node.span);
+ visitSimpleSelector(node);
+ output.writeNode('name', node._name);
+ }
+
+ void visitPseudoElementSelector(PseudoElementSelector node) {
+ output.heading('Pseudo Element Selector', node.span);
+ visitSimpleSelector(node);
+ output.writeNode('name', node._name);
+ }
+
+ void visitNotSelector(NotSelector node) {
+ visitSimpleSelector(node);
+ output.heading('Not Selector', node.span);
+ }
+
+ void visitIdentifier(Identifier node) {
+ output.heading('Identifier(' + output.toValue(node.name) + ")", node.span);
+ }
+
+ // TODO(terry): Defined for frog/tree.dart.
+ void visitTypeReference(lang.TypeReference node) {
+ output.heading('Unimplemented');
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698