1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
|
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = ProjectFileParser.rb -- The TaskJuggler III Project Management Software
#
# Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
# by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
require 'taskjuggler/TextParser'
require 'taskjuggler/ProjectFileScanner'
require 'taskjuggler/TjpSyntaxRules'
require 'taskjuggler/RichText'
require 'taskjuggler/RichText/RTFHandlers'
class TaskJuggler
# This class specializes the TextParser class for use with TaskJuggler project
# files (TJP Files). The primary purpose is to provide functionality that make
# it more comfortable to define the TaskJuggler syntax in a form that is human
# creatable but also powerful enough to define the data structures the parser
# needs to understand the syntax.
#
# By adding some additional information to the syntax rules, we can also
# generate the complete reference manual from this rule set.
class ProjectFileParser < TextParser
include TjpSyntaxRules
# Create the parser object.
def initialize
super
# Define the token types that the ProjectFileScanner may return for
# variable elements.
@variables = [ :INTEGER, :FLOAT, :DATE, :TIME, :STRING, :LITERAL,
:ID, :ID_WITH_COLON, :ABSOLUTE_ID, :MACRO ]
initRules
updateParserTables
@project = nil
end
# Call this function with the master file to start processing a TJP file or
# a set of TJP files.
def open(file, master, fileNameIsBuffer = false)
@scanner = ProjectFileScanner.new(file)
# We need the ProjectFileScanner object for error reporting.
if master && !fileNameIsBuffer && file != '.' && file[-4, 4] != '.tjp'
error('illegal_extension', "Project file name must end with " +
'\'.tjp\' extension')
end
@scanner.open(fileNameIsBuffer)
@property = nil
@scenarioIdx = 0
initFileStack
# Stack for property IDs. Needed to handle nested 'supplement'
# statements.
@idStack = []
end
# Call this function to cleanup the parser structures after the file
# processing has been completed.
def close
@scanner.close
end
# This function will deliver the next token from the scanner. A token is a
# two element Array that contains the ID or type of the token as well as the
# text string of the token.
def nextToken
@scanner.nextToken
end
# This function can be used to return tokens. Returned tokens will be pushed
# on a LIFO stack. To preserve the order of the original tokens the last
# token must be returned first. This mechanism is used to implement
# look-ahead functionality.
def returnToken(token)
@scanner.returnToken(token)
end
# A set of standard marcros is defined in all files as soon as the project
# header has been read. Calling this functions gets the values from @project
# and inserts the Macro objects into the ProjectFileScanner.
def setGlobalMacros
@scanner.addMacro(Macro.new('projectstart', @project['start'].to_s,
@scanner.sourceFileInfo))
@scanner.addMacro(Macro.new('projectend', @project['end'].to_s,
@scanner.sourceFileInfo))
@scanner.addMacro(Macro.new('now', @project['now'].to_s,
@scanner.sourceFileInfo))
@scanner.addMacro(Macro.new('today', @project['now'].
to_s(@project['timeFormat']),
@scanner.sourceFileInfo))
end
def parseReportAttributes(report, attributes)
open(attributes, false, true)
@property = report
@project = report.project
parse(:dynamicAttributes)
end
private
# Utility function that convers English weekday names into their index
# number and does some error checking. It returns 0 for 'sun', 1 for 'mon'
# and so on.
def weekDay(name)
names = %w( sun mon tue wed thu fri sat )
if (day = names.index(@val[0])).nil?
error('weekday', "Weekday name expected (#{names.join(', ')})")
end
day
end
# Make sure that certain attributes are not used after sub properties have
# been added to a property.
def checkContainer(attribute)
if @property.container?
error('container_attribute',
"The attribute #{attribute} may not be used for this property " +
'after sub properties have been added.', @sourceFileInfo[0],
@property)
end
end
# Convenience function to check that an TimeInterval fits completely
# within the project time frame.
def checkInterval(iv)
# Make sure the interval is within the project time frame.
if iv.start < @project['start'] || iv.start >= @project['end']
error('interval_start_in_range',
"Start date #{iv.start} must be within the project time frame " +
"(#{@project['start']} - #{@project['end']})")
end
if iv.end <= @project['start'] || iv.end > @project['end']
error('interval_end_in_range',
"End date #{iv.end} must be within the project time frame " +
"(#{@project['start']} - #{@project['end']})")
end
end
# Convenience function to check the integrity of a booking statement.
def checkBooking(task, resource)
unless task.leaf?
error('booking_no_leaf', "#{task.fullId} is not a leaf task",
@sourceFileInfo[0], task)
end
if task['milestone', @scenarioIdx]
error('booking_milestone', "You cannot add bookings to a milestone",
@sourceFileInfo[0], task)
end
unless resource.leaf?
error('booking_group', "You cannot book a group resource",
@sourceFileInfo[0], task)
end
end
# The TaskJuggler syntax can be extended by the user when the properties are
# extended with user-defined attributes. These attribute definitions
# introduce keywords that have to be processed like the build-in keywords.
# The parser therefor needs to adapt on the fly to the new syntax. By
# calling this function, a TaskJuggler property can be extended with a new
# attribute. @propertySet determines what property should be extended.
# _type_ is the attribute type, _default_ is the default value.
def extendPropertySetDefinition(type, default)
if @propertySet.knownAttribute?(@val[1])
error('extend_redefinition',
"The extended attribute #{@val[1]} has already been defined.")
end
# Determine the values for scenarioSpecific and inheritable.
inherit = false
scenarioSpecific = false
unless @val[3].nil?
@val[3].each do |option|
case option
when 'inherit'
inherit = true
when 'scenariospecific'
scenarioSpecific = true
end
end
end
# Register the new Attribute type with the Property set it should belong
# to.
@propertySet.addAttributeType(AttributeDefinition.new(
@val[1], @val[2], type, inherit, false, scenarioSpecific, default,
true))
# Add the new user-defined attribute as reportable attribute to the parser
# rule.
oldCurrentRule = @cr
@cr = @rules[:reportableAttributes]
unless @cr.include?(@val[1])
singlePattern('_' + @val[1])
descr(@val[2])
end
@cr = oldCurrentRule
scenarioSpecific
end
# This function is primarily a wrapper around the RichText constructor. It
# catches all RichTextScanner processing problems and converts the exception
# data into a MessageHandler message that points to the correct location.
# This is necessary, because the RichText parser knows nothing about the
# actual input file. So we have to map the error location in the RichText
# input stream back to the position in the project file. _sfi_ is the
# SourceFileInfo of the input string. To limit the supported set of
# variable tokens, a subset can be provided by _tokenSet_.
def newRichText(text, sfi, tokenSet = nil)
rText = RichText.new(text, RTFHandlers.create(@project, sfi))
# The RichText is processed by a separate parser. Messages will not have
# the proper source file info unless we baseline them with the original
# source file info.
mh = MessageHandlerInstance.instance
mh.baselineSFI = sfi
rti = rText.generateIntermediateFormat( [ 0, 0, 0 ], tokenSet)
# Reset the baseline again.
mh.baselineSFI = nil
rti.sectionNumbers = false if rti
rti
end
# This method is a convenience wrapper around Report.new. It checks if
# the report name already exists. It also triggers the attribute
# inheritance. +name+ is the name of the report, +type+ is the report
# type. +sourceFileInfo+ is a SourceFileInfo of the report definition. The
# method returns the newly created Report.
def newReport(id, name, type, sourceFileInfo)
# If there is no parent property and the report prefix is not empty, the
# reportprefix defines the parent property.
if @property.nil? && !@reportprefix.empty?
@property = @project.report(@reportprefix)
end
# Report IDs must be unique. If an ID was provided, check if it exists
# already.
if id
# If we have a scope property, we need to prepend the ID of the scope
# property to the provided ID.
id = (@property ? @property.fullId + '.' : '') + @val[1]
if @project.report(id)
error('report_exists', "report #{id} has already been defined.",
sourceFileInfo, @property)
end
end
@reportCounter += 1
if name != '.' && name != ''
if @project.reportByName(name)
error('report_redefinition',
"A report with the name #{name} has already been defined.")
end
end
@property = Report.new(@project, id || "report#{@reportCounter}",
name, @property)
@property.typeSpec = type
@property.sourceFileInfo = sourceFileInfo
@property.inheritAttributes
if block_given?
# The default attribute values for this report type have to be set in
# 'inherited' mode since they are not user provided.
AttributeBase.setMode(1)
yield
AttributeBase.setMode(0)
end
end
# If the @limitResources list is not empty, we have to create a Limits
# object for each Resource. Otherwise, one Limits object is enough.
def setLimit(name, value, interval)
if @limitResources.empty?
@limits.setLimit(name, value, interval)
else
@limitResources.each do |resource|
@limits.setLimit(name, value, interval, resource)
end
end
end
# Set the _attribute_ to _value_ and reset all other duration attributes.
def setDurationAttribute(attribute, value = true)
checkContainer(attribute)
{ 'milestone' => false, 'duration' => 0,
'length' => 0, 'effort' => 0 }.each do |attr, val|
if attribute == attr
@property[attr, @scenarioIdx] = value
else
if @property.getAttribute(attr, @scenarioIdx).provided
error('multiple_durations',
"This duration criteria is overwriting a previously " +
"provided criteria (duration, effort, length or milestone).")
end
@property[attr, @scenarioIdx] = val
end
end
end
# The following functions are mostly conveniance functions to simplify the
# syntax tree definition. The *Rule functions may only be used in _rule
# functions. And only one function call per _rule function is allowed.
# This function creates a set of rules to describe a list of keywords.
# _name_ is the name of the top-level rule and _items_ can be a Hash or
# Array. The array just contains the allowed keywords, the Hash contains
# keyword/description pairs. The description is used to describe
# the keyword in the manual. The syntax supports two special cases. A '*'
# means all items in the list and '-' means the list is empty.
def allOrNothingListRule(name, items)
newRule(name) {
# A '*' means all possible items should be in the list.
pattern(%w( _* ), lambda {
KeywordArray.new([ '*' ])
})
descr('A shortcut for all items')
# A '-' means the list should be empty.
pattern([ '_-' ], lambda {
KeywordArray.new
})
descr('No items')
# Or the list consists of one or more comma separated keywords.
pattern([ "!#{name}_AoN_ruleItems" ], lambda {
KeywordArray.new(@val[0])
})
}
# Create the rule for the comma separated list.
newRule("#{name}_AoN_ruleItems") {
listRule("more#{name}_AoN_ruleItems", "!#{name}_AoN_ruleItem")
}
# Create the rule for the keywords with their description.
newRule("#{name}_AoN_ruleItem") {
if items.is_a?(Array)
items.each { |keyword| singlePattern('_' + keyword) }
else
items.each do |keyword, description|
singlePattern('_' + keyword)
descr(description) if description
end
end
}
end
def listRule(name, listItem)
pattern([ "#{listItem}", "!#{name}" ], lambda {
if @val[1] && @val[1].include?(@val[0])
error('duplicate_in_list',
"Duplicate items in list.")
end
[ @val[0] ] + (@val[1].nil? ? [] : @val[1])
})
newRule(name) {
commaListRule(listItem)
}
end
def commaListRule(listItem)
optional
repeatable
pattern([ '_,', "#{listItem}" ], lambda {
@val[1]
})
end
# Create pattern that turns the rule into the definition for optional
# attributes. _attributes_ is the rule that lists these attributes.
def optionsRule(attributes)
optional
pattern([ '_{', "!#{attributes}", '_}' ], lambda {
@val[1]
})
end
# Create a pattern with just a single _item_. The pattern returns the value
# of that item.
def singlePattern(item)
pattern([ item ], lambda {
@val[0]
})
end
# Add documentation for the current pattern of the currently processed rule.
def doc(keyword, text)
@cr.setDoc(keyword, text)
end
# Add documentation for patterns that only consists of a single terminal
# token.
def descr(text)
if @cr.patterns[-1].length != 1 ||
(@cr.patterns[-1][0][0] != :literal &&
@cr.patterns[-1][0][0] != :variable)
raise 'descr() may only be used for patterns with terminal tokens.'
end
arg(0, nil, text)
end
# Add documentation for the arguments with index _idx_ of the current
# pattern of the currently processed rule. _name_ is that should be used for
# this variable. _text_ is the documentation text.
def arg(idx, name, text)
@cr.setArg(idx, TextParser::TokenDoc.new(name, text))
end
# Restrict the syntax documentation of the previously defined pattern to
# the first +idx+ tokens.
def lastSyntaxToken(idx)
@cr.setLastSyntaxToken(idx)
end
# Specify the support level for the current pattern.
def level(level)
@cr.setSupportLevel(level)
end
# Add a reference to another pattern. This information is only used to
# generate the documentation for the patterns of this rule.
def also(seeAlso)
seeAlso = [ seeAlso ] unless seeAlso.is_a?(Array)
@cr.setSeeAlso(seeAlso)
end
# Add a TJP file or parts of it as an example. The TJP _file_ must be in the
# directory test/TestSuite/Syntax/Correct. _tag_ can be used to identify
# that only a part of the file should be included.
def example(file, tag = nil)
@cr.setExample(file, tag)
end
# Determine the title of the column with the ID _colId_. The title may be
# from the static set or be from a user defined attribute.
def columnTitle(colId)
if @property.typeSpec == :tracereport
"<-id->:<-scenario->.#{colId}"
else
TableReport.defaultColumnTitle(colId) ||
@project.attributeName(colId)
end
end
# To manage certain variables that have file scope throughout a hierachie
# of nested include files, we use a @fileStack to track those variables.
# The values primarily live in their class instance variables. But upon
# return from an included file, we need to restore the old values. This
# function creates or resets the stack.
def initFileStack
@fileStackVariables = %w( taskprefix reportprefix
resourceprefix accountprefix )
stackEntry = {}
@fileStackVariables.each do |var|
stackEntry[var] = ''
instance_variable_set('@' + var, '')
end
@fileStack = [ stackEntry ]
end
# Push a new set of variables onto the @fileStack.
def pushFileStack
stackEntry = {}
@fileStackVariables.each do |var|
stackEntry[var] = instance_variable_get('@' + var)
end
@fileStack << stackEntry
end
# Pop the last stack entry from the @fileStack and restore the class
# variables according to the now top-entry.
def popFileStack
stackEntry = @fileStack.pop
@fileStackVariables.each do |var|
instance_variable_set('@' + var, stackEntry[var])
end
# Include files can only occur at global level or in the project header.
# In both cases, the @property was nil on including and must be reset to
# nil again after the include file.
@property = nil
end
# This method most be used instead of the += operator for all list
# attributes. += will always return an Array object. This will cause
# trouble with the list attributes that are not plain Arrays.
def appendScListAttribute(attrId, list)
list.each do |v|
@property[attrId, @scenarioIdx] << v
end
# The << operator does not set the 'provided' flag. Just do a self
# assignment to trigget the flag to get set.
begin
@property[attrId, @scenarioIdx] = @property[attrId, @scenarioIdx]
rescue AttributeOverwrite
# Overwrites are ok here.
end
end
end
end
|