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

Unified Diff: pkg/analysis_server/tool/spec/codegen_tools.dart

Issue 863883004: Support code generation for other languages (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 11 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analysis_server/tool/spec/codegen_tools.dart
diff --git a/pkg/analysis_server/tool/spec/codegen_tools.dart b/pkg/analysis_server/tool/spec/codegen_tools.dart
index 898f4b777c2d34ae46b43e583015308181872dcf..540bbf5ebe86542bc603cf59243e30fad2bac15e 100644
--- a/pkg/analysis_server/tool/spec/codegen_tools.dart
+++ b/pkg/analysis_server/tool/spec/codegen_tools.dart
@@ -55,6 +55,8 @@ typedef String FileContentsComputer();
*/
class CodeGenerator {
_CodeGeneratorState _state;
+ // Defaults are good for generating Java and Dart source code.
Paul Berry 2015/01/23 19:28:59 It's a little confusing to call this "defaults" si
guillermooo 2015/01/23 19:49:12 Acknowledged.
+ CodeGeneratorSettings _settings = new CodeGeneratorSettings();
/**
* Measure the width of the current indentation level.
@@ -62,6 +64,14 @@ class CodeGenerator {
int get indentWidth => _state.nextIndent.length;
/**
+ * Change code generation settings to adapt [CodeGenerator] to any
Paul Berry 2015/01/23 19:28:59 Since we're allowing the client to change the sett
guillermooo 2015/01/23 19:49:13 Acknowledged.
+ * programming language.
+ */
+ void setCodeGeneratorSettings(CodeGeneratorSettings settings) {
+ _settings = settings;
+ }
+
+ /**
* Execute [callback], collecting any code that is output using [write]
* or [writeln], and return the result as a string.
*/
@@ -82,16 +92,28 @@ class CodeGenerator {
* If [javadocStyle] is true, then the output is compatable with Javadoc,
* which understands certain HTML constructs.
*/
- void docComment(List<dom.Node> docs, {int width: 79, bool javadocStyle:
- false}) {
+ void docComment(List<dom.Node> docs) {
if (containsOnlyWhitespace(docs)) {
return;
}
- writeln('/**');
- indentBy(' * ', () {
+ writeln(_settings.docCommentStart);
+ var width = _settings.commentLineLength;
+ var javadocStyle = _settings.languageName == 'java';
+ indentBy(_settings.docCommentLineLeader, () {
write(nodesToText(docs, width - _state.indent.length, javadocStyle));
});
- writeln(' */');
+ writeln(_settings.docCommentEnd);
+ }
+
+ void lineComment(List<dom.Node> docs) {
+ if (containsOnlyWhitespace(docs)) {
+ return;
+ }
+ write(_settings.lineCommentLineLeader);
+ var width = _settings.commentLineLength;
+ indentBy(_settings.lineCommentLineLeader, () {
+ write(nodesToText(docs, width - _state.indent.length, false));
+ });
}
/**
@@ -126,7 +148,7 @@ class CodeGenerator {
void outputHeader({bool javaStyle: false}) {
String header;
- if (javaStyle) {
+ if (_settings.languageName == 'java') {
header = '''
/*
* Copyright (c) 2014, the Dart project authors.
@@ -144,6 +166,16 @@ class CodeGenerator {
* This file has been automatically generated. Please do not edit it manually.
* To regenerate the file, use the script "pkg/analysis_server/tool/spec/generate_files".
*/''';
+ } else if (_settings.languageName == 'python') {
+ header = '''
+# Copyright (c) 2014, 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.
+#
+# This file has been automatically generated. Please do not edit it manually.
+# To regenerate the file, use the script
+# "pkg/analysis_server/tool/spec/generate_files".
+''';
} else {
header = '''
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
@@ -398,6 +430,47 @@ abstract class HtmlCodeGenerator {
}
/**
+ * Controls several settings of [CodeGenerator].
+ *
+ * The default settings are valid for generating Java and Dart code.
+ */
+class CodeGeneratorSettings {
+ /**
+ * Name of the language being generated. Lowercase.
+ */
+ String languageName;
+
+ /**
+ * Marker used in line comments.
+ */
+ String lineCommentLineLeader;
+
+ /**
+ * Start marker for doc comments.
+ */
+ String docCommentStart;
guillermooo 2015/01/23 19:49:12 Rename to docCommentStartMarker?
+
+ /**
+ * Line leader for body lines in doc comments.
+ */
+ String docCommentLineLeader;
+
+ /**
+ * End marker for doc comments.
+ */
+ String docCommentEnd;
guillermooo 2015/01/23 19:49:13 Rename to docCommentEndMarker?
+
+ /**
+ * Line length for doc comment lines.
+ */
+ int commentLineLength;
guillermooo 2015/01/23 19:49:13 Should we have a `commentLineLength` and `docComme
+
+ CodeGeneratorSettings({this.languageName: 'java', this.lineCommentLineLeader: '// ',
+ this.docCommentStart: '/**', this.docCommentLineLeader: ' * ',
+ this.docCommentEnd: ' */', this.commentLineLength: 79});
+}
+
+/**
* State used by [CodeGenerator].
*/
class _CodeGeneratorState {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698