Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Skeleton code for expanding the AST
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Feb 9, 2015
1 parent 25c97d3 commit 0fc6b11
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/drafter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ fs = require 'fs'
#
class Drafter

# List of data structures
@dataStructures: []

# Default configuration
@defaultConfig:
requireBlueprintName: false # Treat missing API name as error
Expand Down Expand Up @@ -35,7 +38,55 @@ class Drafter
# @param source [String] soruce API Bluerpint code
# @param callback [(Error, ParseResult)]
make: (source, callback) ->
protagonist.parse source, @config, callback
protagonist.parse source, @config, (error, result) =>
callback error if error

ruleList = []
rules = (require './rules/' + rule for rule in ruleList)

@dataStructures = []

@expandNode result.ast, rules, 'blueprint'
callback error, result

# Expand a certain node with the given rules
#
# @param node [Object] A node of API Blueprint
# @param rules [Array] List of rules to apply
# @param elementTye [String] The element type of the node
expandNode: (node, rules, elementType) ->
elementType ?= node.element

# On root node, Gather data structures first before applying rules to any of the children nodes
if elementType is 'blueprint'
for element in node.content

if element.element is 'category'
for subElement in element.content
@dataStructures.push subElement if subElement.element is 'dataStructure'

# Expand the gathered data structures
for rule in rules
rule.dataStructures.call @dataStructures if 'dataStructures' in rule

# Apply rules to the current node
for rule in rules
rule[elementType].call node, @dataStructures if elementType in rule

# Recursively do the same for children nodes
switch elementType
when 'resource'
@expandNode action, rules, 'action' for action in node.actions

when 'action'
@expandNode example, rules, 'transactionExample' for example in node.examples

when 'transactionExample'
@expandNode request, rules, 'payload' for request in node.requests
@expandNode response, rules, 'payload' for response in node.responses

if node.content and Array.isArray node.content
@expandNode element, rules for element in node.content

module.exports = Drafter
module.exports.options = options

0 comments on commit 0fc6b11

Please sign in to comment.