Skip to content

Instantly share code, notes, and snippets.

@StanAngeloff
Created February 28, 2010 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StanAngeloff/317793 to your computer and use it in GitHub Desktop.
Save StanAngeloff/317793 to your computer and use it in GitHub Desktop.
diff --git a/src/grammar.coffee b/src/grammar.coffee
index 0ed752c..40dbc89 100644
--- a/src/grammar.coffee
+++ b/src/grammar.coffee
@@ -75,6 +75,7 @@ grammar: {
o "Class"
o "Splat"
o "Existence"
+ o "Javadoc"
o "Comment"
]
@@ -118,6 +119,7 @@ grammar: {
AssignObj: [
o "Identifier ASSIGN Expression", -> new AssignNode(new ValueNode($1), $3, 'object')
o "AlphaNumeric ASSIGN Expression", -> new AssignNode(new ValueNode($1), $3, 'object')
+ o "Javadoc"
o "Comment"
]
@@ -127,6 +129,11 @@ grammar: {
o "RETURN", -> new ReturnNode(new ValueNode(new LiteralNode('null')))
]
+ # A Javadoc comment.
+ Javadoc: [
+ o "JAVADOC", -> new JavadocNode(yytext)
+ ]
+
# A comment.
Comment: [
o "COMMENT", -> new CommentNode(yytext)
diff --git a/src/lexer.coffee b/src/lexer.coffee
index e824d6e..ce856a3 100644
--- a/src/lexer.coffee
+++ b/src/lexer.coffee
@@ -48,6 +48,7 @@ HEREDOC : /^("{6}|'{6}|"{3}\n?([\s\S]*?)\n?([ \t]*)"{3}|'{3}\n?([\s\S]*?)\n?(
JS : /^(``|`([\s\S]*?)([^\\]|\\\\)`)/
OPERATOR : /^([+\*&|\/\-%=<>:!?]+)/
WHITESPACE : /^([ \t]+)/
+JAVADOC : /^(((\n?[ \t]*)?#{3}[^\n]*)+)/
COMMENT : /^(((\n?[ \t]*)?#[^\n]*)+)/
CODE : /^((-|=)>)/
REGEX : /^(\/(\S.*?)?([^\\]|\\\\)\/[imgy]{0,4})/
@@ -60,6 +61,7 @@ ASSIGNMENT : /^(:|=)$/
JS_CLEANER : /(^`|`$)/g
MULTILINER : /\n/g
STRING_NEWLINES : /\n[ \t]*/g
+JAVADOC_CLEANER : /(^[ \t]*#{3}[ ]?|\n[ \t]*$)/mg
COMMENT_CLEANER : /(^[ \t]*#|\n[ \t]*$)/mg
NO_NEWLINE : /^([+\*&|\/\-%=<>:!.\\][<>=&|]*|and|or|is|isnt|not|delete|typeof|instanceof)$/
HEREDOC_INDENT : /^[ \t]+/mg
@@ -111,6 +113,7 @@ exports.Lexer: class Lexer
return if @js_token()
return if @regex_token()
return if @indent_token()
+ return if @javadoc_token()
return if @comment_token()
return if @whitespace_token()
return @literal_token()
@@ -180,6 +183,15 @@ exports.Lexer: class Lexer
@i += regex.length
true
+ # Matches and consumes Javadoc comments.
+ javadoc_token: ->
+ return false unless comment: @match JAVADOC, 1
+ @line += (comment.match(MULTILINER) or []).length
+ @token 'JAVADOC', comment.replace(JAVADOC_CLEANER, '').split(MULTILINER)
+ @token 'TERMINATOR', "\n"
+ @i += comment.length
+ true
+
# Matches and conumes comments.
comment_token: ->
return false unless comment: @match COMMENT, 1
diff --git a/src/nodes.coffee b/src/nodes.coffee
index fa7816c..856fd4f 100644
--- a/src/nodes.coffee
+++ b/src/nodes.coffee
@@ -65,7 +65,8 @@ exports.BaseNode: class BaseNode
del @options, 'operation' unless @operation_sensitive()
top: if @top_sensitive() then @options.top else del @options, 'top'
closure: @is_statement() and not @is_statement_only() and not top and
- not @options.returns and not (this instanceof CommentNode) and
+ not @options.returns and not
+ (this instanceof CommentNode or this instanceof JavadocNode) and
not @contains (node) -> node.is_statement_only()
if closure then @compile_closure(@options) else @compile_node(@options)
@@ -298,6 +299,23 @@ exports.ValueNode: class ValueNode extends BaseNode
if op and soaked then '(' + complete + ')' else complete
+# Pass through CoffeeScript Javadoc comments into JavaScript Javadoc comments
+# at the same position.
+exports.JavadocNode: class JavadocNode extends BaseNode
+ type: 'Javadoc'
+
+ constructor: (lines) ->
+ @lines: lines
+ this
+
+ compile_node: (o) ->
+ @idt() + '/**' + '\n' +
+ @idt() + ' * ' + @lines.join('\n' + @idt() + ' * ') + '\n' +
+ @idt() + ' */'
+
+statement JavadocNode
+
+
# Pass through CoffeeScript comments into JavaScript comments at the
# same position.
exports.CommentNode: class CommentNode extends BaseNode
### A House class.
### @author Stan Angeloff
class House
### A buy(..) method
### @access public
### @return void
buy: ->
puts 'I am moving soon...'
### func(..) documented
###
### This is a real nice implementation of func1(..)
func1: ->
### Line 1
->
### Line 2
Hello
# Grab all values of `a` and put in `b`
b: a for a in [1..10]
###
empty: ->
empty(above: 1)
empty(below: 2)
###
###no space
### TAB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment