diff -r cbaaa689fab2 -r e6e33e8fe655 PPExpressionParser.st --- a/PPExpressionParser.st Fri May 04 23:58:48 2012 +0200 +++ b/PPExpressionParser.st Fri May 04 23:59:01 2012 +0200 @@ -7,48 +7,6 @@ category:'PetitParser-Tools' ! -PPExpressionParser comment:'A PPExpressionParser is a parser to conveniently define an expression grammar with prefix, postfix, and left- and right-associative infix operators. -The following code initializes a parser for arithmetic expressions. First we instantiate an expression parser, a simple parser for expressions in parenthesis and a simple parser for integer numbers. - expression := PPExpressionParser new. - parens := $( asParser token trim , expression , $) asParser token trim - ==> [ :nodes | nodes second ]. - integer := #digit asParser plus token trim - ==> [ :token | token value asInteger ]. - -Then we define on what term the expression grammar is built on: - expression term: parens / integer. - -Finally we define the operator-groups in descending precedence. Note, that the action blocks receive both, the terms and the parsed operator in the order they appear in the parsed input. - - expression - group: [ :g | - g prefix: $- asParser token trim do: [ :op :a | a negated ] ]; - group: [ :g | - g postfix: ''++'' asParser token trim do: [ :a :op | a + 1 ]. - g postfix: ''--'' asParser token trim do: [ :a :op | a - 1 ] ]; - group: [ :g | - g right: $^ asParser token trim do: [ :a :op :b | a raisedTo: b ] ]; - group: [ :g | - g left: $* asParser token trim do: [ :a :op :b | a * b ]. - g left: $/ asParser token trim do: [ :a :op :b | a / b ] ]; - group: [ :g | - g left: $+ asParser token trim do: [ :a :op :b | a + b ]. - g left: $- asParser token trim do: [ :a :op :b | a - b ] ]. - -After evaluating the above code the ''expression'' is an efficient parser that evaluates examples like: - expression parse: ''-8++''. - expression parse: ''1+2*3''. - expression parse: ''1*2+3''. - expression parse: ''(1+2)*3''. - expression parse: ''8/4/2''. - expression parse: ''8/(4/2)''. - expression parse: ''2^2^3''. - expression parse: ''(2^2)^3''. - -Instance Variables: - operators The operators defined in the current group.' -! - !PPExpressionParser methodsFor:'private'! @@ -76,10 +34,10 @@ ifTrue: [ term ] ifFalse: [ self - perform: selector with: term + perform: selector with: term with: (list size = 1 ifTrue: [ list first first ==> [ :operator | Array with: list first second with: operator ] ] - ifFalse: [ + ifFalse: [ list inject: PPChoiceParser new into: [ :choice :each | choice / (each first ==> [ :operator | Array with: each second with: operator ]) ] ]) ] ] @@ -102,17 +60,17 @@ group: aOneArgumentBlock "Defines a priority group by evaluating aOneArgumentBlock." - + operators := Dictionary new. - parser := [ + parser := [ aOneArgumentBlock value: self. - self buildOn: parser ] + self buildOn: parser ] ensure: [ operators := nil ] ! left: aParser do: aThreeArgumentBlock "Define an operator aParser that is left-associative. Evaluate aThreeArgumentBlock with the first term, the operator, and the second term." - + self operator: #build:left: parser: aParser do: aThreeArgumentBlock ! @@ -130,13 +88,13 @@ right: aParser do: aThreeArgumentBlock "Define an operator aParser that is right-associative. Evaluate aThreeArgumentBlock with the first term, the operator, and the second term." - + self operator: #build:right: parser: aParser do: aThreeArgumentBlock ! term: aParser "Defines the initial term aParser of the receiver." - + parser isNil ifTrue: [ parser := aParser ] ifFalse: [ self error: 'Unable to redefine the term.' ] @@ -144,6 +102,14 @@ !PPExpressionParser class methodsFor:'documentation'! +version + ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPExpressionParser.st,v 1.3 2012-05-04 21:59:01 vrany Exp $' +! + +version_CVS + ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPExpressionParser.st,v 1.3 2012-05-04 21:59:01 vrany Exp $' +! + version_SVN - ^ '$Id: PPExpressionParser.st,v 1.2 2012-01-13 11:22:50 cg Exp $' + ^ '§Id: PPExpressionParser.st 2 2010-12-17 18:44:23Z vranyj1 §' ! !