- A header made up of:
- the word function
- a function name
- an arguments list
- A body made up of a block of code
####
- A header of:
- the word while
- a condition
- A body of a block of code
##
##
- A header of:
- the word for
- enclosed in parenthesis:
- a declaration and then a semicolon
- a condition and then a semi-colon
- an increment
- A body of a block of code
##
##
- A header of the word do
- A body of a block of code
- A footer of:
- the word while
- a condition
##
##
- a header
- a body
- a block of code
- a condition
##
##
$block = qr/
\{
(?:
(?> [^{}]* )
| (??{ $block })
)*
\}
##
##
rule block {
\{
[
<-[{}]>*:
|
]*
\}
}
##
##
rule block {
\(
[
<-[()]>*:
|
]*
\)
}
##
##
rule block ($left,$right) {
$left
[
<-[$left $right]>*:
|
]*
$right
}
##
##
- start quote
- string of (non-quote or backslashed quote) characters
- end quote
##
##
rule quoted_string ($type) {
"
[
<-["]>+:
| [ " ]
]*
"
}
##
##
rule quoted_string ($type) {
$type
[
<-[$type]>+:
| [ $type ]
]*
$type
}
##
##
rule string
{
|
}
##
##
A single-line comment
or
A multi-line comment
##
##
rule single_line_comment {
// \N*:
}
##
##
- A leading /*
- A string of characters that includes non asterix and astrix not followed by a /
- A closing */
##
##
rule multi_line_comment {
/\*
[
<- [*] >* :
|
| \*
]*
\*/
}
##
##
rule block ($left,$right) {
$left
[
<-[$left$right"'/]>*:
|
|
|
]*
$right
}
##
##
@matches = $var =~ /(match)/g;
##
##
$var =~ m:e/@matches:=(match)/;
##
##
#!/usr/bin/perl -w
use strict;
grammar javascript {
rule single_line_comment {
// \N* :
}
rule multi_line_comment {
/\*
[
<- [*] >* :
|
| \*
]*
\*/
}
rule comment {
|
}
rule quoted_string ($type) {
$type
[
<- [$type] >+ :
| [ $type ]
]*
$type
}
rule string {
|
}
rule block ($left,$right) {
$left
[
<- [$left$right"'/] >* :
|
|
|
]*
$right
}
rule code { }
rule cond { }
rule header { }
rule body :w { }
rule match { }
}
grammar script is javascript
{
rule header :wi {
\<
script
<- [>] >* \>
[ \< ! -- ]?
}
rule body :wi {
[
<- [<("'/] >*
|
|
|
|
| >
]*
}
rule footer :wi { \ script \> }
rule match {
##
use re 'eval';
sub quoted_string {
my $type = quotemeta shift;
return qr/
$type
(?:
[^$type]+
| (?: (?<= \\ ) $type )
)*
$type
/x;
}
my $comment =
qr~
(?: // (?> [^\n]* ) ) # single line
| (?: # multi line
/\*
(?:
(?> [^*]+ )
| (?= \*/ )
| \*
)*
\*/
)
~x;
sub block {
my $left = quotemeta shift;
my $right = quotemeta shift;
my $block =
qr!
$left
(?:
(?> [^$left$right"'/] *)
| (??{$comment})
| (??{ quoted_string( qq(") ) })
| (??{ quoted_string( qq(') ) })
| (??{$block})
)*
$right
!x;
return $block;
}
my $arg_list =
qr/
\(
(?:
[^,)]*
(,|)
)*
\)
/x;
# ugly!
my $block;
my $code = qr/ (??{$block = block ( "\{" , "\}" ); "$block"}) /x;
my $cond = qr/ (??{$block = block ( "(" , ")" ); "$block"}) /x;