-------------------------------------------------------------------- |-- Module : Language.WebIDL.Parser-- Copyright : (c) Dmitry Golubovsky, 2009-- License : BSD-style-- -- Maintainer : golubovsky@gmail.com-- Stability : experimental-- Portability : portable-- ------ Parser of the tokenized IDL.------------------------------------------------------------------moduleLanguage.WebIDL.Parser(parseIDL)whereimportData.CharimportHS_LEXER_HimportText.ParserCombinators.ParsecimportText.ParserCombinators.Parsec.PosimportText.ParserCombinators.Parsec.ExprimportLanguage.WebIDL.LexerimportLanguage.WebIDL.SyntaximportControl.MonadtypeIDLParsera=GenParserToken()a-- |Run the parser, return IDL definitions.parseIDL::[Token]->EitherParseErrorIDLSpecificationparseIDLtks=runParserspecification()""tkswherespecification=doss<-manydefinitiontokEOFreturn(filternodefppss)nodefpp(IDLDefinition__IDLDefPP)=Falsenodefpp_=Truedefinition=dojd<-tryjavadocpos<-getPositiondf<-definition'return$IDLDefinitionposjddfdefinition'=try(withSemidef_module)<|>trydef_interface<|>try(withSemiexcept_dcl>>=return.IDLDefExcept)<|>try(withSemitype_dcl>>=return.IDLDefType)<|>try(withSemiconst_dcl>>=return.IDLDefConst)<|>try(withSemivalue_dcl)<|>try(preprocessor>>returnIDLDefPP)def_module=doos<-option[](tryextended_attribute_list)tokMODULEi<-tokIDENTIFIER>>=return.namedfs<-curlies(manydefinition)return$IDLDefModuleiosdfsjavadoc=option""(try(tokJAVADOC>>=return.name))>>=return.JavaDocdef_interface=doi<-(tryinterface_dcl<|>tryforward_dcl)tokSemireturniexcept_dcl=dotokEXCEPTIONei<-tokIDENTIFIER>>=return.nameml<-curlies(manymember)return$IDLExceptDcleimlforward_dcl=dotokINTERFACEii<-tokIDENTIFIER>>=return.namereturn$IDLDefInterfaceii[][]Nothinginterface_dcl=doh<-interface_headerb<-curliesinterface_bodyreturn$h$Justbinterface_header=doeas<-option[](tryextended_attribute_list)tokINTERFACEii<-tokIDENTIFIER>>=return.nameinhr<-option[](tryinterface_inheritance_spec)return$IDLDefInterfaceiieasinhrinterface_body=does<-manyexportreturn$IDLInterfaceBody(filternoexpppes)noexppp(IDLExport__IDLExpPP)=Falsenoexppp_=Trueexport=dojd<-javadocpos<-getPositionex<-export'return$IDLExportposjdexexport'=try(withSemitype_dcl>>=return.IDLExpType)<|>try(withSemiexcept_dcl>>=return.IDLExpExcept)<|>try(withSemiattr_dcl>>=return.IDLExpAttr)<|>try(withSemiop_dcl>>=return.IDLExpOp)<|>try(withSemiconst_dcl>>=return.IDLExpConst)<|>try(preprocessor>>returnIDLExpPP)type_dcl=trytdnative<|>try(struct_type>>=return.IDLStruct)<|>trytdtypedef<|>try(tokSTRUCT>>tokIDENTIFIER>>=return.IDLConstrFwd.name)<?>"type declaration"value_dcl=dotokVALUETYPEvi<-id_or_domstringmt<-optionNothing(trytype_spec>>=return.Just)return$IDLDefValuevimttdnative=dotokNATIVEtn<-tokIDENTIFIER>>=return.namereturn$IDLNativetntdtypedef=dotokTYPEDEFts<-type_specds<-(declarator`sepBy1`tokComma)return$IDLTypeDeftsdsop_dcl=doeas<-option[](tryextended_attribute_list)oao<-optionNothing(trytokONEWAY>>return(JustIDLOneWay))ots<-op_type_specoi<-tokIDENTIFIER>>=return.namepds<-parameter_dclsrsx<-option[](tryraises_expr)return$IDLOpDcleasoaootsoipdsrsxraises_expr=tokRAISES>>parens(scoped_name`sepBy1`tokComma)interface_inheritance_spec=dotokColonscoped_name`sepBy1`tokCommaop_type_spec=tryparam_type_spec<|>try(tokVOID>>returnIDLParamVoid)<?>"operation type specification"param_type_spec=try(scoped_name>>=return.IDLParamScopedSpec)<|>try(base_type_spec>>=return.IDLParamBaseSpec)<|>try(string_type>>=return.IDLParamStringSpec)<?>"parameter type specification"simple_type_spec=try(scoped_name>>=return.IDLSimpleScoped)<|>try(base_type_spec>>=return.IDLSimpleBase)<|>try(template_type_spec>>=return.IDLSimpleTmpl)<?>"simple type specification"base_type_spec=try(floating_pt_type>>=return.IDLBaseTypeFloat)<|>try(integer_type>>=return.IDLBaseTypeInt)<|>try(tokCHAR>>returnIDLBaseTypeChar)<|>try(tokBOOLEAN>>returnIDLBaseTypeBool)<|>try(tokOCTET>>returnIDLBaseTypeOctet)<|>try(tokANY>>returnIDLBaseTypeAny)template_type_spec=try(sequence_type>>=return.IDLTmplSequence)<|>try(string_type>>=return.IDLTmplString)<|>try(fixed_pt_type>>=return.IDLTmplFixed)floating_pt_type=try(tokLONG>>tokDOUBLE>>returnIDLLongDouble)<|>try(tokFLOAT>>returnIDLFloat)<|>try(tokDOUBLE>>returnIDLDouble)<?>"floating point type"sequence_type=dotokSEQUENCEangles$dost<-simple_type_specmbc<-optionNothing(tokComma>>const_exp>>=return.Just)return$IDLSequenceTypestmbcfixed_pt_type=dotokFIXEDangles$doc1<-const_expc2<-const_expreturn$IDLFixedTypec1c2integer_type=dosus<-optionIDLSigned(try(tokUNSIGNED>>returnIDLUnsigned))it<-try(tokSHORT>>returnIDLShortInt)<|>try(tokLONG>>tokLONG>>returnIDLLongLongInt)<|>try(tokLONG>>returnIDLLongInt)<?>"integer type"return$susitscoped_name=doouter<-optionFalse(trytokOP_SCOPE>>returnTrue)ns<-id_or_domstring`sepBy1`tokOP_SCOPEreturn$IDLScopedNameouternsextended_attribute_list=brackets(extended_attribute`sepBy1`tokComma)extended_attribute=dojd<-javadocai<-tokIDENTIFIER>>=return.nameds<-optionNothing(extended_attribute_details>>=return.Just)return$IDLExtAttrjdaidsextended_attribute_details=tryeadid<|>tryeadsn<|>try(parameter_dcls>>=return.IDLDetailPD)eadsn=dotokEqsn<-scoped_namereturn$IDLDetailSNsneadid=dotokEqei<-tokIDENTIFIER>>=return.namepds<-parameter_dclsreturn$IDLDetailIDeipdsparameter_dcls=parens(param_dcl`sepBy`tokComma)param_dcl=doeas<-option[](tryextended_attribute_list)pa<-param_attributept<-param_type_specpi<-tokIDENTIFIER>>=return.namereturn$IDLParamDcleaspaptpiparam_attribute=try(tokIN>>returnIDLParamIn)<|>try(tokOUT>>returnIDLParamOut)<|>try(tokINOUT>>returnIDLParamInOut)member=dots<-type_specds<-(declarator`sepBy1`tokComma)tokSemireturn$IDLMembertsdstype_spec=try(simple_type_spec>>=return.IDLSimpleSpec)<|>try(struct_type>>=return.IDLStructSpec)struct_type=dotokSTRUCTsi<-tokIDENTIFIER>>=return.nameml<-curlies(manymember)return$IDLStructTypesimlstring_type=dotokSTRINGsl<-optionNothing(anglesconst_exp>>=return.Just)return$IDLStringTypesldeclarator=try(array_declarator>>=return.IDLComplexDecl)<|>try(simple_declarator>>=return.IDLSimpleDecl)<?>"declarator"array_declarator=doai<-tokIDENTIFIER>>=return.namedims<-many1fixed_array_sizereturn$IDLArrayDeclaratoraidimsfixed_array_size=bracketsconst_expattr_dcl=doos<-option[](tryextended_attribute_list)ro<-optionFalse(try(tokREADONLY>>returnTrue))tokATTRIBUTEps<-param_type_specai<-tokIDENTIFIER>>=return.namegx<-x_excep_exprtokGETRAISESsx<-x_excep_exprtokSETRAISESreturn$IDLAttrDclosropsaigxsxx_excep_exprtok=option[]$try$dotokparens(scoped_name`sepBy1`tokComma)const_dcl=dotokCONSTct<-const_typeci<-tokIDENTIFIER>>=return.nametokEqcc<-const_expreturn$IDLConstDclctciccconst_type=try(floating_pt_type>>=return.IDLConstTypeFloat)<|>try(integer_type>>=return.IDLConstTypeInt)<|>try(tokCHAR>>returnIDLConstTypeChar)<|>try(tokBOOLEAN>>returnIDLConstTypeBool)<|>try(tokOCTET>>returnIDLConstTypeOctet)<|>try(scoped_name>>=return.IDLConstTypeScoped)<|>try(string_type>>=return.IDLConstTypeString)<|>try(tokFIXED>>returnIDLConstTypeFixed)const_exp=buildExpressionParseroptableprimary_exproptable=[[pfxc'-'IDLNeg,pfxc'+'IDLPos,pfxc'~'IDLNot],[binc'*'IDLMultAssocLeft,binc'/'IDLDivAssocLeft,binc'%'IDLRemAssocLeft],[binc'+'IDLAddAssocLeft,binc'-'IDLSubAssocLeft],[binarytokOP_SHLIDLShiftLAssocLeft,binarytokOP_SHRIDLShiftRAssocLeft],[binc'&'IDLAndAssocLeft],[binc'^'IDLXorAssocLeft],[binc'|'IDLOrAssocLeft]]binarytokctorassoc=Infix(tok>>return(IDLBinExpctor))assocprefixtokctor=Prefix(tok>>return(IDLUnaryExpctor))pfxcc=prefix(tokCharc)bincc=binary(tokCharc)primary_expr=try(scoped_name>>=return.IDLPrimScoped)<|>try(parensconst_exp>>=return.IDLParenExp)<|>try(literal>>=return.IDLPrimLit)literal=try(tokValc_INTEGER_LITERAL"<integer>">>=return.IDLIntLit)<|>try(tokValc_STRING_LITERAL"<string>">>=return.IDLStringLit)<|>try(tokValc_CHARACTER_LITERAL"<char>">>=return.IDLCharLit)<|>try(tokValc_FIXED_PT_LITERAL"<fixed>">>=return.IDLFixedLit)<|>try(tokValc_FLOATING_PT_LITERAL"<float>">>=return.IDLFloatLit)<|>try(tokTRUE>>return(IDLBoolLit"TRUE"))<|>try(tokFALSE>>return(IDLBoolLit"FALSE"))<?>"literal"simple_declarator=id_or_domstring-- There is one marginal case when DOMString acts as a declarator.-- This results in the tokSTRING to occur in the place of the declarator.-- The following code takes care of that, so "typedef dom::DOMString DOMString"-- does not crash the parser.id_or_domstring=try(tokSTRING>>return"DOMString")<|>try(tokIDENTIFIER>>=return.name)-- Preprocessor directives: ignore everything between pound-sign and EOL.-- Sometimes the last line contains #endif, and then EOF without EOL.-- For this case, both EOL and EOF are checked, and if EOF is encountered,-- it is returned for reprocessing, but EOL is simulated.preprocessor=dotokPOUND_SIGNmanyTillanyTokeneol_eofeol_eof=try(tokEOL>>returntokEOL)<|>try(dot<-tokEOFsetInput[t]returntokEOL)-- Tokens from the lexertokPost=newPos""(linet)(columnt)-- Universal token tester.tokTok::Integraln=>n->String->IDLParserTokentokTokns=token(consts)tokPosisn<?>swhereisnt=if(fromIntegraln)==tagtthen(Justt)elseNothing-- One-character token.tokChar::Char->IDLParserChartokCharc=token(const[c])tokPos(iscc)<?>[c]whereiscct=if(ordc)==tagtthen(Justc)elseNothing-- A token whose value has to be retrieved.tokVal::Integraln=>n->String->IDLParserStringtokValns=tokTokns>>=return.name-- Token primitives. They pass when a token specified is encountered, fail otherwise.tokEOF=tokTok0"<eof>"tokMODULE=tokTokc_MODULE"module"tokJAVADOC=tokTokc_JAVADOC"/** javadoc */"tokIDENTIFIER=tokTokc_IDENTIFIER"identifier"tokINTERFACE=tokTokc_INTERFACE"interface"tokOP_SCOPE=tokTokc_OP_SCOPE"::"tokTYPEDEF=tokTokc_TYPEDEF"typedef"tokNATIVE=tokTokc_NATIVE"native"tokONEWAY=tokTokc_ONEWAY"oneway"tokRAISES=tokTokc_RAISES"raises"tokVOID=tokTokc_VOID"void"tokIN=tokTokc_IN"in"tokOUT=tokTokc_OUT"out"tokINOUT=tokTokc_INOUT"inout"tokEXCEPTION=tokTokc_EXCEPTION"exception"tokSTRUCT=tokTokc_STRUCT"struct"tokCHAR=tokTokc_CHAR"char"tokBOOLEAN=tokTokc_BOOLEAN"boolean"tokOCTET=tokTokc_OCTET"octet"tokANY=tokTokc_ANY"any"tokLONG=tokTokc_LONG"long"tokFLOAT=tokTokc_FLOAT"float"tokDOUBLE=tokTokc_DOUBLE"double"tokUNSIGNED=tokTokc_UNSIGNED"unsigned"tokSHORT=tokTokc_SHORT"short"tokREADONLY=tokTokc_READONLY"readonly"tokATTRIBUTE=tokTokc_ATTRIBUTE"attribute"tokGETRAISES=tokTokc_GETRAISES"getraises"tokSETRAISES=tokTokc_SETRAISES"setraises"tokCONST=tokTokc_CONST"const"tokFIXED=tokTokc_FIXED"const"tokSTRING=tokTokc_STRING"string"tokOP_SHR=tokTokc_OP_SHR">>"tokOP_SHL=tokTokc_OP_SHL"<<"tokTRUE=tokTokc_TRUE"True"tokFALSE=tokTokc_FALSE"False"tokSEQUENCE=tokTokc_SEQUENCE"sequence"tokVALUETYPE=tokTokc_VALUETYPE"valuetype"tokPOUND_SIGN=tokTokc_POUND_SIGN"#"tokEOL=tokTokc_EOL"<eol>"tokSemi=tokChar';'tokColon=tokChar':'tokComma=tokChar','tokEq=tokChar'='-- Various utilitiescurlies=between(tokChar'{')(tokChar'}')brackets=between(tokChar'[')(tokChar']')parens=between(tokChar'(')(tokChar')')angles=between(tokChar'<')(tokChar'>')withSemip=dox<-ptokSemireturnx