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

Unified Diff: frog/css/tree_css.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: 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: frog/css/tree_css.dart
diff --git a/frog/css/tree_css.dart b/frog/css/tree_css.dart
new file mode 100644
index 0000000000000000000000000000000000000000..8075deb59c84714d8bcc4f8c15c7ebf25952711a
--- /dev/null
+++ b/frog/css/tree_css.dart
@@ -0,0 +1,258 @@
+// 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:
+/////////////////////////////////////////////////////////////////////////
+
+
+// TODO(terry): Type and TypeParameter defined for frog/tree.dart.
+class Type { }
+class TypeParameter extends Node { }
+
+
+class Identifier extends Node {
+ String name;
+
+ Identifier(this.name, SourceSpan span): super(span) {}
+
+ visit(TreeVisitor visitor) => visitor.visitIdentifier(this);
+
+ String toString() => name;
+}
+
+class SelectorGroup extends 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<Node> selectors;
nweiz 2011/11/10 00:04:39 Why isn't this List<SimpleSelector>?
terry 2011/11/22 16:40:47 Should be. Done.
+
+ SelectorGroup(this.selectors, 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();
+ }
+}
+
nweiz 2011/11/10 00:04:39 As mentioned elsewhere, there should be a represen
terry 2011/11/22 16:40:47 Yep added combinator descendant (space) and NONE.
+/* All other selectors (element, #id, .class, attribute, pseudo, negation,
+ * namespace, *) are derived from this selector.
+ */
+class SimpleSelector extends Node {
+ Token _combinator; // +, >, ~ or NONE (space)
nweiz 2011/11/10 00:04:39 Related to the sequence of simple selector issue,
terry 2011/11/22 16:40:47 I have for now maybe I'm being too pedantic (even
+
+ // name is of type IdSelector, ClassSelector, ElementSelector,
+ // PseudoClassSelector, PseudoElementSelector, NotSelector, or Attribute
nweiz 2011/11/10 00:04:39 Is this really true? Aren't all these subclasses o
terry 2011/11/22 16:40:47 No comment is outdated. On 2011/11/10 00:04:39, nw
+ 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;
nweiz 2011/11/10 00:04:39 Ah, so _name is an Identifier or a String. Why doe
terry 2011/11/22 16:40:47 It's an identifier or wildcard nothing else String
+
+ 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, [Token combinator = TokenKind.COMBINATOR_NONE]) :
+ super(name, combinator);
+
+ bool isWildcard() => name == '*';
nweiz 2011/11/10 00:04:39 A universal selector is not really the same thing
terry 2011/11/22 16:40:47 Your right I removed it when I added notion of wil
+
+ visit(TreeVisitor visitor) => visitor.visitElementSelector(this);
+
+ String toString() => "$name";
+}
+
+// namespace|element
+class NamespaceSelector extends SimpleSelector {
+ var _namespace; // null, '*' or identifier name
+
+ NamespaceSelector(this._namespace, String name,
+ [Token combinator = TokenKind.COMBINATOR_NONE]) : super(name, combinator);
nweiz 2011/11/10 00:04:39 I really don't like the overloading of _name that'
terry 2011/11/22 16:40:47 Right it's an identifier or wildcard. On 2011/11/1
+
+ String get namespace() => _namespace is Identifier ?
nweiz 2011/11/10 00:04:39 Same comment as SimpleSelector._name above.
terry 2011/11/22 16:40:47 namespace is never a string either Identifier or W
+ _namespace.name : _namespace;
+
+ bool isWildcardElement() => _name.isWildcard();
nweiz 2011/11/10 00:04:39 Why is this something that goes through NamespaceS
terry 2011/11/22 16:40:47 Shouldn't it's on simpleSelector. On 2011/11/10 00
+
+ 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, [Token combinator = TokenKind.COMBINATOR_NONE]) :
+ super(name, combinator);
+
+ visit(TreeVisitor visitor) => visitor.visitIdSelector(this);
+
+ String toString() => "#$name";
+}
+
+// .class
+class ClassSelector extends SimpleSelector {
+ ClassSelector(String name, [Token combinator = TokenKind.COMBINATOR_NONE]) :
+ super(name, combinator);
+
+ visit(TreeVisitor visitor) => visitor.visitClassSelector(this);
+
+ String toString() => ".$name";
+}
+
+// :pseudoClass
+class PseudoClassSelector extends SimpleSelector {
+ PseudoClassSelector(String name,
+ [Token combinator = TokenKind.COMBINATOR_NONE]) : super(name, combinator);
+
+ visit(TreeVisitor visitor) => visitor.visitPseudoClassSelector(this);
+
+ String toString() => ":$name";
+}
+
+// ::pseudoElement
+class PseudoElementSelector extends SimpleSelector {
+ PseudoElementSelector(String name,
+ [Token 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, [Token combinator = TokenKind.COMBINATOR_NONE]) :
+ super(name, combinator);
+
+ visit(TreeVisitor visitor) => visitor.visitNotSelector(this);
+}
+
+// TODO(terry): Implement
+// [attribute]
+class Attribute extends 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(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.writeNode('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(TypeReference node) {
+ output.heading('Unimplemented');
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698