.NAME Escape sequences

.DESCRIPTION
Only those escape sequences that are defined in 
ISO/IEC 14882:2003 shall be used. 

.COMMENT V7.0

.SEVERITY Required

.COMMENT
Please refer to the MISRA C++:2008 document for further details (www.misra-cpp.com)

.COMMAND tcl

.CODE

::logiscope::include -once common utilities.tcl

proc compute {standardName tree} {
  variable CurrentParameterList
  # Look for forbidden escape sequences
  utilities::filterTree $tree [list [namespace current]::check]
}

proc check { node } {
  set curOperator [::logiscope::treeOperator $node]
  set curOperatorName [::logiscope::opName $curOperator]

  # Look for a string
  if {[string equal $curOperatorName string]} {
    set curAtom [::logiscope::treeAtomValue $node]
    set stringToCheck [::logiscope::stringValue $curAtom]

	# Look for octal escape sequence (\123)
	# They should not start with digits 8 or 9 
    set foundMatches [regexp -inline -all  {\\[8-9].*} $stringToCheck]
	foreach curMatch $foundMatches {
 	    ::logiscope::addViolation $node
	    return 0
	}	

	# Look for hexadecimal escape sequence (\x123)
	# They should start with an hexadecimal digit
    set foundMatches [regexp -inline -all  {\\x[^0-9A-Fa-f].*} $stringToCheck]
	foreach curMatch $foundMatches {
 	    ::logiscope::addViolation $node
	    return 0
	}	

	
	# Look for forbidden sequences (a backslash and a forbidden character)
	# Note that the x0-7 characters have been added to the search since they've already been processed
    set foundMatches [regexp -inline -all  {.\\[^ntvbrfa\\\?'"x0-7]} $stringToCheck]
	foreach curMatch $foundMatches {
	  # Check that the forbidden match does not in fact start with a double backslash.
	  # in this case, it's not a violation
	  # Note that the x0-7 characters have been added to the search since they've already been processed
	  if {![regexp {.*(\\\\[^ntvbrfa\\\?'"x0-7]).*} $curMatch]} {
 	    ::logiscope::addViolation $node
	    return 0
	  }
	}	
  }
  return 1
}
