| Cobra | Interactive Query Language | pattern set management | ||
|---|---|---|---|---|
| NAMEps -- pattern set managementSYNTAXps help # version 3.9 and later ps caption name message... # version 3.9 and later ps create name ps convert name ps delete name ps exists name # version 4.6 and later ps list [name] ps rename oldname newname # version 3.9 and later ps name = name [& | ^ + - * m < > ( )] name ps name = name with (constraint) # version 4.8 and later ps name = name with "string" # version 4.8 and later DESCRIPTIONThe command: ps helplists a summary of commands that are handled by the ps command. The command ps caption name messagecan be used to attach an explanatory message to an existing pattern set 'name'. The message field can be any length. A named pattern set can be created with the pe command, or it can be created by converting an existing set of token markings into a structured and named pattern set with the create option, as follows: : ps create SetnameThis command leaves the exising markings in tact, but stores a copy as a named set where it can later be combined with other sets or have its elements displayed separately with a dp command. The reverse operation, converting a set of structured matches into basic markings while leaving the set unmodified is performed by the convert option. : ps convert nameThe tokens that are part of the pattern are marked with a combination of binary values, ranging from 1 to 9, as detailed under Notes in pe. The first token of the pattern further has its .bound field set to point to the last token, which means that when marked tokens are printed with any of the d, l, or p commands, it will always print the complete range, even if some tokens in between were later unmarked. To prevent this, the .bound field must be set to a differnt token reference (e.g., .bound = .). The list option lists the available sets (or a specific one) together with the number of patterns that each set contains. The delete option omits the named set. : ps delete nameThe exists command checks if a given named set exists and is non-empty, and stops a script execution if it does not. If the command is issued interactively from the command prompt, Cobra exits if the named set does not exist or is empty. : ps exists nameWhen placed in a script this command is useful to stop a script execution, for instance, if an earlier pe command did not produce any matches, so that no further processing of the remainder of the script is needed. In this sense the command is similar in effect to a failed requires command. The list command displays non-empty sets. : ps list [name]Lists all, or a specific, set of patterns that are currently stored. : ps rename oldname newnameRenames an existing pattern set. The ps command can also be used to create new derived sets by applying standard set operations to other named sets. The three most important commands in this set can be used to define set union, difference, or intersection. But, there are six additional set operations, inspired by the operators from Allen Interval Logic (Cobra version 4.1 and later). When defining set operations, the first name given is followed (after a space) by an assignment operator (=) and two existing setnames separated by one of the valid operators. : ps C = A & BDefines a new set C that contains the intersection of existing sets A and B (i.e., only those elements that appear in both sets). : ps C = A + B # or A | BDefines a new set C that contains the union of A and B. : ps C = A - B # or A ^ BDefines a new set C that contains the elements of A that do not also appear in named set B. The additional set operations inspired by Allen interval logic are as follows. Note that a match from a pattern expression defines an interval in the code, with a start, an end, and possibly some tokens in between. So we can ask for matches to be contained in one another or to overlap, etc. The operations are: : ps C = A * B # or A d B (for A during B)Matches in set B appear inside matches from set A. If the matches are single tokens, rather then intervals, then this checks for equality, but if at least the matches from set A are longer intervals, we can look for appearances in set B that are contained in matches from set A. : ps C = A m B # A meets BMatches in set B that start immediately following the end of a match in set A. : ps C = A < B # A precedes BMatches in set A that precede a match in set B, with no overlap, by any distance. : ps C = A > B # B follows AMatches in set A that follow a match in set B, with no overlap, by any distance. : ps C = A ( B # A starts before and ends during BMatches in set A that start before, but end inside a match in set B. : ps C = A ) B # A starts during and ends after BMatches in set A that start during, but end after a match in set B. Finally, we can also use the ps command to create a subset of matches stored in a new named set, containing only matches from a given set that contain a specific text-string, or matching a specific expression on the token attributes of the first lexical token in a match. : ps C = A with "for" # match contains at least one for statement : ps D = A with "k" # match contains at least one use of variable k : ps E = A with (.fnm == "foo.c" && .lnr < 100)The ps with is new in Version 4.8 and similar to the use of constraints in inline Cobra programs with the builtin function pset. EXAMPLES
  $ cobra *.c	# cobra sources
  : pe A: for ( x:@ident .* ) { .* :x = .* }
  9 patterns stored in set 'A'
  : dp A 1	# display first pattern matched
  ...
  : pe B: for ( .* ) { .* emalloc .* }
  7 patterns stored in set 'B'
  : dp B 1	# display first pattern matched
  ...
   : ps C = A & B  	# set intersection
   : ps D = A + B  	# set union
   : ps E = A - B  	# set difference
   : ps F = B - A
   : ps convert F	# convert the patterns in set C to markings
   : pe X: for ( .* )
   : ps Y = X m X	# nested for-loops
   : pe N: @int
   : ps M = X * N	# for loop controls containing declarations of integers
   : ps O = X with "if"	# matches containing at least one if statement
SEE ALSOpattern searches, dp, ps, pe/pat, | ||||
| Return to index Manual Tutorial | (Last Updated: 26 June 2024) | |||