- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
c# barcode generator library open source PARSERS BECAUSE BNF IS NOT JUST FOR ACADEMICS ANYMORE in Font
CHAPTER 8 PARSERS BECAUSE BNF IS NOT JUST FOR ACADEMICS ANYMORE Data Matrix 2d Barcode Encoder In None Using Barcode generator for Font Control to generate, create Data Matrix image in Font applications. www.OnBarcode.comPrint Code 39 In None Using Barcode generation for Font Control to generate, create USS Code 39 image in Font applications. www.OnBarcode.com/* EscapeCharacter :: SingleEscapeCharacter DecimalDigit x u */ lazy val escapeCharacter: Parser[Unit] = (singleEscapeCharacter | decimalDigit | 'x' | 'u') ^^^ () /* HexEscapeSequence :: x HexDigit HexDigit */ lazy val hexEscapeSequence: Parser[Char] = 'x' ~> hexDigit ~ hexDigit ^^ {case d1 ~ d2 => Integer.parseInt(d1.toString + d2, 16).toChar} /* UnicodeEscapeSequence :: u HexDigit HexDigit HexDigit HexDigit */ lazy val unicodeEscapeSequence = 'u' ~> hexDigit ~ hexDigit ~ hexDigit ~ hexDigit ^^ {case d1 ~ d2 ~ d3 ~ d4 => Integer.parseInt(d1.toString + d2 + d3 + d4, 16).toChar} /* ArrayLiteral : [ Elision(opt) ] [ ElementList ] [ ElementList , Elision(opt) ] */ lazy val arrayLiteral: Parser[List[Any]] = spaces ~ '[' ~ spaces ~> elementList <~ spaces ~ opt(elision) ~ spaces ~ ']' ~ spaces /* ElementList : Elision(opt) AssignmentExpression ElementList , Elision(opt) AssignmentExpression */ Creating UPC Symbol In None Using Barcode drawer for Font Control to generate, create UPC Symbol image in Font applications. www.OnBarcode.comUSS-128 Printer In None Using Barcode encoder for Font Control to generate, create GS1-128 image in Font applications. www.OnBarcode.comCHAPTER 8 PARSERS BECAUSE BNF IS NOT JUST FOR ACADEMICS ANYMORE
Print Barcode In None Using Barcode encoder for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.comEncode QR In None Using Barcode generator for Font Control to generate, create QR-Code image in Font applications. www.OnBarcode.comlazy val elementList: Parser[List[Any]] = repsep(spaces ~> jsonObject, elision) /* Elision : , Elision , */ lazy val elision: Parser[Unit] = rep1(spaces ~ ',' ~ spaces) ^^^ () /* ObjectLiteral : { } { PropertyNameAndValueList } */ lazy val objectLiteral: Parser[Map[String, Any]] = spaces ~ '{' ~ spaces ~ '}' ~ spaces ^^^ Map[String, Any]() | spaces ~ '{' ~ spaces ~> propertyNameAndValueList <~ spaces ~'}' ~ spaces ^^ (vl => Map(vl :_*)) /* PropertyNameAndValueList : PropertyName : AssignmentExpression PropertyNameAndValueList , PropertyName : AssignmentExpression */ lazy val propertyNameAndValueList:Parser[List[(String, Any)]] = rep1sep((spaces ~> propertyName) ~ (spaces ~ ':' ~ spaces ~> jsonObject) ^^ { case n ~ v => (n, v)}, spaces ~ ',' ~ spaces) /* PropertyName : Identifier StringLiteral NumericLiteral */ lazy val propertyName: Parser[String] = stringLiteral | numericLiteral ^^ (_.longValue.toString) | identifier Barcode Printer In None Using Barcode creator for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.comPaint RoyalMail4SCC In None Using Barcode maker for Font Control to generate, create Royal Mail Barcode image in Font applications. www.OnBarcode.comCHAPTER 8 PARSERS BECAUSE BNF IS NOT JUST FOR ACADEMICS ANYMORE
Draw ECC200 In None Using Barcode maker for Online Control to generate, create Data Matrix image in Online applications. www.OnBarcode.comData Matrix ECC200 Printer In Visual Studio .NET Using Barcode printer for VS .NET Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications. www.OnBarcode.com/* IdentifierName :: IdentifierStart IdentifierName IdentifierPart */ lazy val identifier: Parser[String] = identifierStart | identifier ~ identifierPart ^^ { case a ~ b => a+b } /* IdentifierStart :: UnicodeLetter $ _ */ lazy val identifierStart: Parser[String] = '$' ^^^ "$" | '_' ^^^ "_" | unicodeLetter ^^ (_.toString) /* IdentifierPart :: IdentifierStart UnicodeDigit */ lazy val identifierPart: Parser[String] = identifierStart | unicodeDigit ^^ (_.toString) lazy val unicodeLetter = elem("Letter", Character.isLetter) lazy val unicodeDigit = elem("Letter", Character.isDigit) lazy val jsonObject: Parser[Any] = objectLiteral | arrayLiteral | literal type RootType = Any def root = jsonObject } GS1 DataBar-14 Encoder In Java Using Barcode maker for Java Control to generate, create GS1 DataBar Expanded image in Java applications. www.OnBarcode.comPainting Barcode In Java Using Barcode drawer for Java Control to generate, create Barcode image in Java applications. www.OnBarcode.comCHAPTER 8 PARSERS BECAUSE BNF IS NOT JUST FOR ACADEMICS ANYMORE
PDF-417 2d Barcode Encoder In None Using Barcode creator for Online Control to generate, create PDF-417 2d barcode image in Online applications. www.OnBarcode.comLinear 1D Barcode Maker In Java Using Barcode generation for Java Control to generate, create Linear Barcode image in Java applications. www.OnBarcode.comSo, that s it. Let s see how the Parser does with various JSON input: Create Barcode In Objective-C Using Barcode creator for iPhone Control to generate, create Barcode image in iPhone applications. www.OnBarcode.comData Matrix Scanner In C# Using Barcode decoder for .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comscala> JSON.run("1") Barcode Recognizer In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comPDF417 Maker In None Using Barcode creation for Excel Control to generate, create PDF 417 image in Excel applications. www.OnBarcode.comres0: JSON.ParseResult[JSON.RootType] = [1.2] parsed: 1.0 Scanning Barcode In Java Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications. www.OnBarcode.comPDF-417 2d Barcode Creator In VS .NET Using Barcode creation for Reporting Service Control to generate, create PDF 417 image in Reporting Service applications. www.OnBarcode.comscala> JSON.run("'Elwood Eats Mice'") res1: JSON.ParseResult[JSON.RootType] = [1.19] parsed: Elwood Eats Mice
scala> JSON.run("'Elwood \u0021 Eats Mice'") res2: JSON.ParseResult[JSON.RootType] = [1.21] parsed: Elwood ! Eats Mice
scala> JSON.run("[1,2,3,]") res3: JSON.ParseResult[JSON.RootType] = [1.9] parsed: List(1.0, 2.0, 3.0) scala> JSON.run("{a:true, 'hello':33}") res4: parsed: Map(a -> true, hello -> 33.0) Great. We ve converted the ECMAScript spec into a running Parser. Let s see what we can layer on top of the JSON Parser. Let s build some code that will parse Twitter APIs. Twitter JSON Parsing
JSON is great for storing and transmitting dynamically typed information, but Scala is a statically typed language. It would be great to be able to convert the dynamic stuff parsed from JSON data into statically typed Scala. As an example, we re going to use some of the CHAPTER 8 PARSERS BECAUSE BNF IS NOT JUST FOR ACADEMICS ANYMORE
Twitter APIs that return Twitter s public timeline.6 We ll request the data in JSON format, parse it, and then run the parsed data through a conversion to create Scala instances that represent the Twitter data. Utilities
In order to use Scala s for comprehension to safely and easily extract data from the result of the JSON parsing, we re going to write a helper trait, SafeMap, which does type-safe testing and casting. SafeMap has an is method that takes an implicit parameter, man, which is a Manifest representing the type T. The Scala compiler correctly builds the Manifest and passes it to the method.7 We use the Manifest to get the class for T and do testing. First we have the code in Listing 8-3 and then a dissection. Listing 8-3. Type-Safe Map Utility trait SafeMap { import scala.reflect._ class SafeCast(in: Any) { def is[T](implicit man: Manifest[T]): Option[T] = { // special case for Boolean val cls = if (man.toString == "boolean") classOf[java.lang.Boolean] else man.erasure in match { case null => None case t: AnyRef if cls.isAssignableFrom(t.getClass) => Some(t.asInstanceOf[T]) case _ => None } } } 6. http://apiwiki.twitter.com/REST+API+Documentation. Alex Payne, co-author with Dean Wampler of the fine book Programming Scala (O Reilly, forthcoming), maintains Twitter s APIs. 7. Manifest is an experimental feature introduced in Scala 2.7.2, but it s so very useful that I m including it in the book.
|
|