Computers / Programming / Projects / Code Formatter / Keyword Set

Keyword sets are defined in set elements n the type’s xml file. They are used to indicate sets of words that should be highlighted in some way. For example most programming languages have sets of reserved words that have special meaning in that language.

c.xml
<set name="Type Keywords" colour="purple" regEx="[a-zA-Z]">
<startTypeIds>
<startType type="c" id="0" />
</startTypeIds>
<words>void char short int long double float signed unsigned</words>
</set>
54
55
56
57
58
59

The set element has a set of attributes that define how the set works. The startType elements define the type name and state id combination in which the keywords are to be applied. For example keywords aren’t highlighted in comments.

Attribute Description Default Value
name The descriptive name of the state “”
colour The colour to use for the state “”
regEx regular expression used to ensure only whole words are detected "a^"
wildRegEx regular expression used to find wild card words “[a-zA-Z0-9]”
words The set of words to be highlighted. The special “~word” sequence indicates that any word is allowed. This might appear after other characters in which case the prefix must match and then the rest of the word is included  

Code

Set.h

h type icon
Type: Header file
Language: C++
Set.h

Set.cpp

cpp type icon
Type: Code file
Language: C++
Set.cpp

The Set class stores information about sets of keywords. It contains a method to try to find and print a word from the set.

The FindAndPrintSetWord(std::stringstream& lineStream, std::string line, int pos, TypeIdPair testType, char preC, State currentState) method tests to see if a word from this set exists at the specified pos in line and is applicable given the current Type and State ID pair. If a word exists then it gets added to the lineStream. It starts by testing preC (previous character) against the regEx to prevent sub-words from getting highlighted. It then loops through the list of allowed start type name and state ids to see if any of them match the passed in pair. If the set is applicable it loops through the defined words. If the word is only “~word” then it returns a word based on the regEx. If “~word” is a part of a larger word it checks to see if the first part matches and then tries to get a word using the wildRegEx. If the word doesn’t contain “~word” it simply tests to see if the word matches the line. If a word is found then the currentState span is closed if required, the word is added to the line and the currentState span is restarted if required. The length of the word is returned with 0 indicating no word has been printed.