Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Parser for a simple grammar that describes a tree structure using a function- | 6 * Parser for a simple grammar that describes a tree structure using a function- |
| 7 * like "a(b(c,d))" syntax. Original intended usage: to have browsertests | 7 * like "a(b(c,d))" syntax. Original intended usage: to have browsertests |
| 8 * specify an arbitrary tree of iframes, loaded from various sites, without | 8 * specify an arbitrary tree of iframes, loaded from various sites, without |
| 9 * having to write a .html page for each level or do crazy feats of data: url | 9 * having to write a .html page for each level or do crazy feats of data: url |
| 10 * escaping. But there's nothing really iframe-specific here. See below for some | 10 * escaping. But there's nothing really iframe-specific here. See below for some |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 29 * // { value: 'g', children: [ | 29 * // { value: 'g', children: [ |
| 30 * // { value: 'h', children: [] }, | 30 * // { value: 'h', children: [] }, |
| 31 * // { value: 'i', children: [ | 31 * // { value: 'i', children: [ |
| 32 * // { value: 'j', children: [] } | 32 * // { value: 'j', children: [] } |
| 33 * // ]}, | 33 * // ]}, |
| 34 * // ]} | 34 * // ]} |
| 35 * // ]} | 35 * // ]} |
| 36 * // ]} | 36 * // ]} |
| 37 * TreeParserUtil.parse('e(f(g(h(),i(j))))'; | 37 * TreeParserUtil.parse('e(f(g(h(),i(j))))'; |
| 38 * | 38 * |
| 39 * @example <caption>Tree nodes can also have attributes, which are a comma- | |
| 40 * separated list of identifiers, surrounded by braces, after the node name. | |
| 41 * </caption> | |
| 42 * // returns { value: 'b', attributes: [red,blue], children: [ | |
| 43 * // { value: 'c', children: [] }, | |
| 44 * // { value: 'd', attributes: [green], children: [] } | |
| 45 * // ]} | |
| 46 * TreeParserUtil.parse('b{red,blue}(c,d{green})'; | |
|
ncarter (slow)
2017/05/05 22:34:05
So it turns out that I did write unittests for thi
iclelland
2017/05/06 03:03:10
Thanks! I will definitely take a look.
| |
| 47 * | |
| 39 * @example <caption>flatten() converts a [sub]tree back to a string.</caption> | 48 * @example <caption>flatten() converts a [sub]tree back to a string.</caption> |
| 40 * var tree = TreeParserUtil.parse('b.com (c.com(e.com), d.com)'); | 49 * var tree = TreeParserUtil.parse('b.com (c.com(e.com), d.com)'); |
| 41 * TreeParserUtil.flatten(tree.children[0]); // returns 'c.com(e.com())' | 50 * TreeParserUtil.flatten(tree.children[0]); // returns 'c.com(e.com())' |
| 42 */ | 51 */ |
| 43 var TreeParserUtil = (function() { | 52 var TreeParserUtil = (function() { |
| 44 'use strict'; | 53 'use strict'; |
| 45 | 54 |
| 46 /** | 55 /** |
| 47 * Parses an input string into a tree. See class comment for examples. | 56 * Parses an input string into a tree. See class comment for examples. |
| 48 * @returns A tree of the form {value: <string>, children: <Array.<tree>>}. | 57 * @returns A tree of the form {value: <string>, children: <Array.<tree>>}. |
| 49 */ | 58 */ |
| 50 function parse(input) { | 59 function parse(input) { |
| 51 var tokenStream = lex(input); | 60 var tokenStream = lex(input); |
| 52 | 61 |
| 53 var result = takeIdAndChild(tokenStream); | 62 var result = takeIdAndChild(tokenStream); |
| 54 if (tokenStream.length != 0) | 63 if (tokenStream.length != 0) |
| 55 throw new Error('Expected end of stream, but found "' + | 64 throw new Error('Expected end of stream, but found "' + |
| 56 tokenStream[0] + '".') | 65 tokenStream[0] + '".') |
| 57 return result; | 66 return result; |
| 58 } | 67 } |
| 59 | 68 |
| 60 /** | 69 /** |
| 61 * Inverse of |parse|. Converts a parsed tree object into a string. Can be | 70 * Inverse of |parse|. Converts a parsed tree object into a string. Can be |
| 62 * used to forward a subtree as an argument to a nested document. | 71 * used to forward a subtree as an argument to a nested document. |
| 63 */ | 72 */ |
| 64 function flatten(tree) { | 73 function flatten(tree) { |
| 65 return tree.value + '(' + tree.children.map(flatten).join(',') + ')'; | 74 var result = tree.value; |
| 75 if (tree.attributes && tree.attributes.length) | |
| 76 result += '{' + tree.attributes.join(",") + "}"; | |
| 77 return result + '(' + tree.children.map(flatten).join(',') + ')'; | |
| 66 } | 78 } |
| 67 | 79 |
| 68 /** | 80 /** |
| 69 * Lexer function to convert an input string into a token stream. Splits the | 81 * Lexer function to convert an input string into a token stream. Splits the |
| 70 * input along whitespace, parens and commas. Whitespace is removed, while | 82 * input along whitespace, parens and commas. Whitespace is removed, while |
| 71 * parens and commas are preserved as standalone tokens. | 83 * parens and commas are preserved as standalone tokens. |
| 72 * | 84 * |
| 73 * @param {string} input The input string. | 85 * @param {string} input The input string. |
| 74 * @return {Array.<string>} The resulting token stream. | 86 * @return {Array.<string>} The resulting token stream. |
| 75 */ | 87 */ |
| 76 function lex(input) { | 88 function lex(input) { |
| 77 return input.split(/(\s+|\(|\)|,)/).reduce( | 89 return input.split(/(\s+|\(|\)|{|}|,)/).reduce( |
| 78 function (resultArray, token) { | 90 function (resultArray, token) { |
| 79 var trimmed = token.trim(); | 91 var trimmed = token.trim(); |
| 80 if (trimmed) { | 92 if (trimmed) { |
| 81 resultArray.push(trimmed); | 93 resultArray.push(trimmed); |
| 82 } | 94 } |
| 83 return resultArray; | 95 return resultArray; |
| 84 }, []); | 96 }, []); |
| 85 } | 97 } |
| 86 | 98 |
| 87 | |
| 88 /** | 99 /** |
| 89 * Consumes from the stream an identifier and optional child list, returning | 100 * Consumes from the stream an identifier and optional child list, returning |
| 90 * its parsed representation. | 101 * its parsed representation. |
| 91 */ | 102 */ |
| 92 function takeIdAndChild(tokenStream) { | 103 function takeIdAndChild(tokenStream) { |
| 93 return { value: takeIdentifier(tokenStream), | 104 return { value: takeIdentifier(tokenStream), |
| 105 attributes: takeAttributeList(tokenStream), | |
|
ncarter (slow)
2017/05/05 22:34:05
The jsdoc suggests that attributes here will be om
iclelland
2017/05/06 03:03:10
Updated.
| |
| 94 children: takeChildList(tokenStream) }; | 106 children: takeChildList(tokenStream) }; |
| 95 } | 107 } |
| 96 | 108 |
| 97 /** | 109 /** |
| 98 * Consumes from the stream an identifier, returning its value (as a string). | 110 * Consumes from the stream an identifier, returning its value (as a string). |
| 99 */ | 111 */ |
| 100 function takeIdentifier(tokenStream) { | 112 function takeIdentifier(tokenStream) { |
| 101 if (tokenStream.length == 0) | 113 if (tokenStream.length == 0) |
| 102 throw new Error('Expected an identifier, but found end-of-stream.'); | 114 throw new Error('Expected an identifier, but found end-of-stream.'); |
| 103 var token = tokenStream.shift(); | 115 var token = tokenStream.shift(); |
| 104 if (!token.match(/[a-zA-Z0-9.-]+/)) | 116 if (!token.match(/^[a-zA-Z0-9.-]+$/)) |
| 105 throw new Error('Expected an identifier, but found "' + token + '".'); | 117 throw new Error('Expected an identifier, but found "' + token + '".'); |
| 106 return token; | 118 return token; |
| 107 } | 119 } |
| 108 | 120 |
| 109 /** | 121 /** |
| 122 * Consumes an optional attribute list from the token stream, returning a list | |
| 123 * of the parsed attribute identifiers. | |
| 124 */ | |
| 125 function takeAttributeList(tokenStream) { | |
| 126 // Remove the next token from the stream if it matches |token|. | |
| 127 function tryToEatA(token) { | |
| 128 if (tokenStream[0] == token) { | |
| 129 tokenStream.shift(); | |
| 130 return true; | |
| 131 } | |
| 132 return false; | |
| 133 } | |
| 134 | |
| 135 // Bare identifier case, as in 'b' in the input '(a (b, c))' | |
| 136 if (!tryToEatA('{')) | |
| 137 return []; | |
| 138 | |
| 139 // Empty list case, as in 'b' in the input 'a (b {}, c)'. | |
| 140 if (tryToEatA('}')) { | |
| 141 return []; | |
| 142 } | |
| 143 | |
| 144 // List with at least one entry. | |
| 145 var result = [ takeIdentifier(tokenStream) ]; | |
| 146 | |
| 147 // Additional entries allowed with comman. | |
|
ncarter (slow)
2017/05/05 22:34:05
comman -> comma
iclelland
2017/05/06 03:03:10
Thanks, done
| |
| 148 while (tryToEatA(',')) { | |
| 149 result.push(takeIdentifier(tokenStream)); | |
| 150 } | |
| 151 | |
| 152 // End of list. | |
| 153 if (tryToEatA('}')) { | |
| 154 return result; | |
| 155 } | |
| 156 if (tokenStream.length == 0) | |
| 157 throw new Error('Expected "}" or ",", but found end-of-stream.'); | |
| 158 throw new Error('Expected "}" or ",", but found "' + tokenStream[0] + '".'); | |
| 159 } | |
| 160 | |
| 161 /** | |
| 110 * Consumes an optional child list from the token stream, returning a list of | 162 * Consumes an optional child list from the token stream, returning a list of |
| 111 * the parsed children. | 163 * the parsed children. |
| 112 */ | 164 */ |
| 113 function takeChildList(tokenStream) { | 165 function takeChildList(tokenStream) { |
| 114 // Remove the next token from the stream if it matches |token|. | 166 // Remove the next token from the stream if it matches |token|. |
| 115 function tryToEatA(token) { | 167 function tryToEatA(token) { |
| 116 if (tokenStream[0] == token) { | 168 if (tokenStream[0] == token) { |
| 117 tokenStream.shift(); | 169 tokenStream.shift(); |
| 118 return true; | 170 return true; |
| 119 } | 171 } |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 144 if (tokenStream.length == 0) | 196 if (tokenStream.length == 0) |
| 145 throw new Error('Expected ")" or ",", but found end-of-stream.'); | 197 throw new Error('Expected ")" or ",", but found end-of-stream.'); |
| 146 throw new Error('Expected ")" or ",", but found "' + tokenStream[0] + '".'); | 198 throw new Error('Expected ")" or ",", but found "' + tokenStream[0] + '".'); |
| 147 } | 199 } |
| 148 | 200 |
| 149 return { | 201 return { |
| 150 parse: parse, | 202 parse: parse, |
| 151 flatten: flatten | 203 flatten: flatten |
| 152 }; | 204 }; |
| 153 })(); | 205 })(); |
| OLD | NEW |