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

Unified Diff: tools/json_comment_eater/json_comment_eater.py

Issue 13599004: Fix some low hanging inefficiencies in the docs server (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 8 months 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: tools/json_comment_eater/json_comment_eater.py
diff --git a/tools/json_comment_eater/json_comment_eater.py b/tools/json_comment_eater/json_comment_eater.py
new file mode 100644
index 0000000000000000000000000000000000000000..85874649980de182f0342da2344c23554fa892dd
--- /dev/null
+++ b/tools/json_comment_eater/json_comment_eater.py
@@ -0,0 +1,60 @@
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+'''Utility to remove comments from JSON files so that they can be parsed by
+json.loads.
+'''
+
+def _Rcount(string, chars):
+ '''Returns the number of consecutive characters from |chars| that occur at the
+ end of |string|.
+ '''
+ return len(string) - len(string.rstrip(chars))
+
+def _FindNextToken(string, tokens, start):
+ '''Finds the next token in |tokens| that occurs in |string| from |start|.
+ Returns a tuple (index, token key).
+ '''
+ min_index, min_key = (-1, None)
+ for k in tokens:
+ index = string.find(k, start)
+ if index != -1 and (min_index == -1 or index < min_index):
+ min_index, min_key = (index, k)
+ return (min_index, min_key)
+
+def _ReadString(input, start, output):
+ output.append('"')
+ start_range, end_range = (start, input.find('"', start))
+ # \" escapes the ", \\" doesn't, \\\" does, etc.
+ while (end_range != -1 and
+ _Rcount(input[start_range:end_range], '\\') % 2 == 1):
+ start_range, end_range = (end_range, input.find('"', end_range + 1))
+ if end_range == -1:
+ return start_range + 1
+ output.append(input[start:end_range + 1])
+ return end_range + 1
+
+def _ReadComment(input, start, output):
+ eol_tokens = ('\n', '\r')
+ eol_token_index, eol_token = _FindNextToken(input, eol_tokens, start)
+ if eol_token is None:
+ return len(input)
+ output.append(eol_token)
+ return eol_token_index + len(eol_token)
+
+def Nom(input):
+ token_actions = {
+ '"': _ReadString,
+ '//': _ReadComment,
+ }
+ output = []
+ pos = 0
+ while pos < len(input):
+ token_index, token = _FindNextToken(input, token_actions.keys(), pos)
+ if token is None:
+ output.append(input[pos:])
+ break
+ output.append(input[pos:token_index])
+ pos = token_actions[token](input, token_index + len(token), output)
+ return ''.join(output)
« no previous file with comments | « tools/json_comment_eater/everything_expected.json ('k') | tools/json_comment_eater/json_comment_eater_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698