Parent

Class Index [+]

Quicksearch

TaskJuggler::SyntaxReference

This class can traverse the syntax rules of the ProjectFileParser and extract all documented keywords including their arguments and relations. All this work in done in the contructor. The documentation can then be generated for all found keyword or just a single one. Currently plain text output as well as HTML files are supported.

Attributes

keywords[R]

Public Class Methods

new(manual = nil) click to toggle source

The constructor is the most important function of this class. It creates a parser object and then traverses all rules and extracts the documented patterns. In a second pass the extracted KeywordDocumentation objects are then cross referenced to capture their relationships. manual is an optional reference to the UserManual object that uses this SyntaxReference.

    # File lib/SyntaxReference.rb, line 35
35:     def initialize(manual = nil)
36:       @manual = manual
37:       @messageHandler = MessageHandler.new(true)
38:       @parser = ProjectFileParser.new(@messageHandler)
39:       @parser.updateParserTables
40: 
41:       # This hash stores all documented keywords using the keyword as
42:       # index.
43:       @keywords = {}
44:       @parser.rules.each_value do |rule|
45:         rule.patterns.each do |pattern|
46:           #  Only patterns that are documented are of interest.
47:           next if pattern.doc.nil?
48: 
49:           # Make sure each keyword is unique.
50:           if @keywords.include?(pattern.keyword)
51:             raise "Multiple patterns have the same keyword #{pattern.keyword}"
52:           end
53: 
54:           argDocs = []
55:           # Create a new KeywordDocumentation object and fill-in all extracted
56:           # values.
57:           kwd = KeywordDocumentation.new(rule, pattern,
58:                   pattern.to_syntax(argDocs, @parser.rules), argDocs,
59:                   optionalAttributes(pattern, {}), @manual)
60:           @keywords[pattern.keyword] = kwd
61:         end
62:       end
63: 
64:       # Make sure all references to other keywords are present.
65:       @keywords.each_value do |kwd|
66:         kwd.crossReference(@keywords, @parser.rules)
67:       end
68: 
69:       # Figure out whether the keyword describes an inheritable attribute or
70:       # not.
71:       @keywords.each_value do |kwd|
72:         kwd.computeInheritance(@keywords, @parser.rules)
73:       end
74:     end

Public Instance Methods

all() click to toggle source

Return a sorted Array with all keywords (as String objects).

    # File lib/SyntaxReference.rb, line 77
77:     def all
78:       sorted = @keywords.keys.sort
79:       # Register the neighbours with each keyword so we can use this info in
80:       # navigation bars.
81:       pred = nil
82:       sorted.each do |kwd|
83:         keyword = @keywords[kwd]
84:         pred.successor = keyword if pred
85:         keyword.predecessor = pred
86:         pred = keyword
87:       end
88:     end
generateHTMLnavbar(directory, keywords) click to toggle source

Generate 2 files named navbar.html and alphabet.html. They are used to support navigating through the syntax reference.

     # File lib/SyntaxReference.rb, line 141
141:     def generateHTMLnavbar(directory, keywords)
142:       html = HTMLDocument.new
143:       head = html.generateHead('TaskJuggler Syntax Reference Navigator')
144:       head << XMLElement.new('base', 'target' => 'display')
145:       html << (body = XMLElement.new('body'))
146: 
147:       body << XMLNamedText.new('Table Of Contents', 'a', 'href' => 'toc.html')
148:       body << XMLElement.new('br')
149: 
150:       normalizedKeywords = {}
151:       keywords.each do |keyword|
152:         normalizedKeywords[@keywords[keyword].title] = keyword
153:       end
154:       letter = nil
155:       letters = []
156:       normalizedKeywords.keys.sort!.each do |normalized|
157:         if normalized[0, 1] != letter
158:           letter = normalized[0, 1]
159:           letters << letter
160:           body << (h = XMLElement.new('h3'))
161:           h << XMLNamedText.new(letter.upcase, 'a', 'name' => letter)
162:         end
163:         keyword = normalizedKeywords[normalized]
164:         body << XMLNamedText.new("#{normalized}", 'a',
165:                                  'href' => "#{keyword}.html")
166:         body << XMLElement.new('br')
167:       end
168: 
169:       html.write(directory + 'navbar.html')
170: 
171:       html = HTMLDocument.new
172:       head = html.generateHead('TaskJuggler Syntax Reference Navigator')
173:       head << XMLElement.new('base', 'target' => 'navigator')
174:       html << (body = XMLElement.new('body'))
175: 
176:       body << (h3 = XMLElement.new('h3'))
177:       letters.each do |l|
178:         h3 << XMLNamedText.new(l.upcase, 'a',
179:                                'href' => "navbar.html##{l}")
180:       end
181:       html.write(directory + 'alphabet.html')
182:     end
generateHTMLreference(directory, keyword) click to toggle source

Generate a documentation for the keyword or an error message. The result is a XML String for known keywords. In case of an error the result is empty but an error message will be send to $stderr.

     # File lib/SyntaxReference.rb, line 131
131:     def generateHTMLreference(directory, keyword)
132:       if checkKeyword(keyword)
133:         @keywords[keyword].generateHTML(directory)
134:       else
135:         ''
136:       end
137:     end
internalReferences() click to toggle source
     # File lib/SyntaxReference.rb, line 108
108:     def internalReferences
109:       references = {}
110:       @keywords.each_value do |keyword|
111:         (refs = keyword.references.uniq).empty? ||
112:           references[keyword.keyword] = refs
113:       end
114:       references
115:     end
tableOfContents(toc, sectionPrefix) click to toggle source

Generate entries for a TableOfContents for each of the keywords. The entries are appended to the TableOfContents toc. sectionPrefix is the prefix that is used for the chapter numbers. In case we have 20 keywords and sectionPrefix is ‘A’, the keywords will be enumerated ‘A.1’ to ‘A.20’.

     # File lib/SyntaxReference.rb, line 95
 95:     def tableOfContents(toc, sectionPrefix)
 96:       keywords = all
 97:       # Set the chapter name to 'Syntax Reference' with a link to the first
 98:       # keyword.
 99:       toc.addEntry(TOCEntry.new(sectionPrefix, 'Syntax Reference', keywords[0]))
100:       i = 1
101:       keywords.each do |keyword|
102:         title = @keywords[keyword].title
103:         toc.addEntry(TOCEntry.new("#{sectionPrefix}.#{i}", title, keyword))
104:         i += 1
105:       end
106:     end
to_s(keyword) click to toggle source

Generate a documentation for the keyword or an error message. The result is a multi-line plain text String for known keywords. In case of an error the result is empty but an error message will be send to $stderr.

     # File lib/SyntaxReference.rb, line 120
120:     def to_s(keyword)
121:       if checkKeyword(keyword)
122:         @keywords[keyword].to_s
123:       else
124:         ''
125:       end
126:     end

Private Instance Methods

attributes(token, scenarioSpecific) click to toggle source

For the rule referenced by token all patterns are collected that define the terminal token of each first token of each pattern of the specified rule. The patterns are returned as a hash. For each pattern the hashed boolean value specifies whether the attribute is scenario specific or not.

     # File lib/SyntaxReference.rb, line 224
224:     def attributes(token, scenarioSpecific)
225:       raise "Token #{token} must reference a rule" if token[0] != !!
226:       token = token.slice(1, token.length - 1)
227:       # Find the matching rule.
228:       rule = @parser.rules[token]
229:       attrs = {}
230:       # Now we look at the first token of each pattern.
231:       rule.patterns.each do |pattern|
232:         if pattern[0][0] == __
233:           # If it's a terminal symbol, we found what we are looking for. We add
234:           # it to the attrs hash and mark it as non scenario specific.
235:           attrs[pattern] = scenarioSpecific
236:         elsif pattern[0] == '!scenarioIdCol'
237:           # A reference to the !scenarioId rule marks the next token of the
238:           # pattern as a reference to a rule with all scenario specific
239:           # attributes.
240:           attrs.merge!(attributes(pattern[1], true))
241:         elsif pattern[0][0] == !!
242:           # In case we have a reference to another rule, we just follow the
243:           # reference. If the pattern is documented we don't have to follow the
244:           # reference. We can use the pattern instead.
245:           if pattern.doc.nil?
246:             attrs.merge!(attributes(pattern[0], scenarioSpecific))
247:           else
248:             attrs[pattern] = scenarioSpecific
249:           end
250:         else
251:           raise "Hit unknown token #{token}"
252:         end
253:       end
254:       attrs
255:     end
checkKeyword(keyword) click to toggle source
     # File lib/SyntaxReference.rb, line 257
257:     def checkKeyword(keyword)
258:       if keyword.nil? || @keywords[keyword].nil?
259:         unless keyword.nil?
260:           $stderr.puts "ERROR: #{keyword} is not a known keyword.\n\n"
261:         end
262:         # Create list of top-level keywords.
263:         kwdStr = ''
264:         @keywords.each_value do |kwd|
265:           if kwd.contexts.empty? ||
266:              (kwd.contexts.length == 1 && kwd.contexts[0] == kwd)
267:             kwdStr += ', ' unless kwdStr.empty?
268:             kwdStr += kwd.keyword
269:           end
270:         end
271:         $stderr.puts "Try one of the following keywords as argument to this " +
272:                      "program:\n"
273:         $stderr.puts "#{kwdStr}"
274:         return false
275:       end
276: 
277:       true
278:     end
optionalAttributes(pattern, stack) click to toggle source

Find optional attributes and return them hashed by the defining pattern.

     # File lib/SyntaxReference.rb, line 187
187:     def optionalAttributes(pattern, stack)
188:       # If we hit an endless recursion we won't find any attributes. So we push
189:       # each pattern we process on the 'stack'. If we hit it again, we just
190:       # return an empty hash.
191:       return {} if stack[pattern]
192: 
193:       # If we hit a pattern that is documented, we ignore it.
194:       return {} if !stack.empty? && pattern.doc
195: 
196:       # Push pattern onto 'stack'.
197:       stack[pattern] = true
198: 
199:       if pattern[0] == '_{' && pattern[2] == '_}'
200:         # We have found an optional attribute pattern!
201:         return attributes(pattern[1], false)
202:       end
203: 
204:       # If a token of the pattern is a reference, we recursively
205:       # follow the reference to the next pattern.
206:       pattern.each do |token|
207:         if token[0] == !!
208:           token = token.slice(1, token.length - 1)
209:           rule = @parser.rules[token]
210:           # Rules with multiple patterns won't lead to attributes.
211:           next if rule.patterns.length > 1
212: 
213:           attrs = optionalAttributes(rule.patterns[0], stack)
214:           return attrs unless attrs.empty?
215:         end
216:       end
217:       {}
218:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.