Example #1
0
/// \brief Create an in-place ASCII string stream as input to ANTLR 3.
///
/// An in-place string steam is the preferred method of supplying strings to ANTLR as input 
/// for lexing and compiling. This is because we make no copies of the input string but
/// read from it right where it is.
///
/// \param[in] inString	Pointer to the string to be used as the input stream
/// \param[in] size	Size (in 8 bit ASCII characters) of the input string
/// \param[in] name	NAme to attach the input stream (can be NULL pointer)
///
/// \return
///	- Pointer to new input stream context upon success
///	- One of the ANTLR3_ERR_ defines on error.
///
/// \remark
///  - ANTLR does not alter the input string in any way.
///  - String is slightly incorrect in that the passed in pointer can be to any
///    memory in C version of ANTLR3 of course.
////
ANTLR3_API pANTLR3_INPUT_STREAM	
antlr3NewAsciiStringInPlaceStream   (pANTLR3_UINT8 inString, ANTLR3_UINT32 size, pANTLR3_UINT8 name)
{
	// Pointer to the input stream we are going to create
	//
	pANTLR3_INPUT_STREAM    input;

	// Allocate memory for the input stream structure
	//
	input   = (pANTLR3_INPUT_STREAM)
					ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM));

	if	(input == NULL)
	{
		return	NULL;
	}

	// Structure was allocated correctly, now we can install the pointer.
	//
	input->isAllocated	= ANTLR3_FALSE;
	input->data			= inString;
	input->sizeBuf		= size;

	// Call the common 8 bit ASCII input stream handler initializer.
	//
	antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);

	// Now we can set up the file name
	//
	input->istream->streamName	= input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)"-memory-" : name);
	input->fileName				= input->istream->streamName;

	return  input;
}
Example #2
0
/** \brief Use the contents of an operating system file as the input
 *         for an input stream.
 *
 * \param fileName Name of operating system file to read.
 * \return
 *	- Pointer to new input stream context upon success
 *	- One of the ANTLR3_ERR_ defines on error.
 */
ANTLR3_API pANTLR3_INPUT_STREAM
antlr3AsciiFileStreamNew(pANTLR3_UINT8 fileName)
{
	// Pointer to the input stream we are going to create
	//
	pANTLR3_INPUT_STREAM    input;
	ANTLR3_UINT32	    status;

	if	(fileName == NULL)
	{
		return NULL;
	}

	// Allocate memory for the input stream structure
	//
	input   = (pANTLR3_INPUT_STREAM)
		ANTLR3_CALLOC(1, sizeof(ANTLR3_INPUT_STREAM));

	if	(input == NULL)
	{
		return	NULL;
	}

	// Structure was allocated correctly, now we can read the file.
	//
	status  = antlr3readAscii(input, fileName);

	// Call the common 8 bit ASCII input stream handler
	// Initializer type thingy doobry function.
	//
	antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);

	// Now we can set up the file name
	//	
	input->istream->streamName	= input->strFactory->newStr(input->strFactory, fileName);
	input->fileName				= input->istream->streamName;

	if	(status != ANTLR3_SUCCESS)
	{
		input->close(input);
		return	NULL;
	}

	return  input;
}
static pANTLR3_INPUT_STREAM
antlr3CreateLineBufferedStream(std::istream& in)
{
	// Pointer to the input stream we are going to create
	//
	pANTLR3_INPUT_STREAM    input;

	if	(!in)
	{
		return NULL;
	}

	// Allocate memory for the input stream structure
	//
	input   = (pANTLR3_INPUT_STREAM)
		ANTLR3_CALLOC(1, sizeof(ANTLR3_LINE_BUFFERED_INPUT_STREAM));

	if	(input == NULL)
	{
		return	NULL;
	}

	// Structure was allocated correctly, now we can install the pointer
	//
        input->data             = malloc(1024);
        input->isAllocated	= ANTLR3_FALSE;

        ((pANTLR3_LINE_BUFFERED_INPUT_STREAM)input)->in = ∈

	// Call the common 8 bit input stream handler
	// initialization.
	//
#ifdef CVC4_ANTLR3_OLD_INPUT_STREAM
	antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);
#else /* CVC4_ANTLR3_OLD_INPUT_STREAM */
	antlr38BitSetupStream(input);
        // In some libantlr3c 3.4-beta versions, this call is not included in the above.
        // This is probably an erroneously-deleted line in the libantlr3c source since 3.2.
	antlr3GenericSetupStream(input);
#endif /* CVC4_ANTLR3_OLD_INPUT_STREAM */

        return  input;
}
Example #4
0
/// \brief Create an ASCII string stream as input to ANTLR 3, copying the input string.
///
/// This string stream first makes a copy of the string at the supplied pointer
///
/// \param[in] inString	Pointer to the string to be copied as the input stream
/// \param[in] size	Size (in 8 bit ASCII characters) of the input string
/// \param[in] name	NAme to attach the input stream (can be NULL pointer)
///
/// \return
///	- Pointer to new input stream context upon success
///	- One of the ANTLR3_ERR_ defines on error.
///
/// \remark
///  - ANTLR does not alter the input string in any way.
///  - String is slightly incorrect in that the passed in pointer can be to any
///    memory in C version of ANTLR3 of course.
////
pANTLR3_INPUT_STREAM	antlr3NewAsciiStringCopyStream	    (pANTLR3_UINT8 inString, ANTLR3_UINT32 size, pANTLR3_UINT8 name)
{
	// Pointer to the input stream we are going to create
	//
	pANTLR3_INPUT_STREAM    input;

	// Allocate memory for the input stream structure
	//
	input   = (pANTLR3_INPUT_STREAM)
		ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM));

	if	(input == NULL)
	{
		return	NULL;
	}

	// Indicate that we allocated this input and allocate it
	//
	input->isAllocated	    = ANTLR3_TRUE;
	input->data		    = ANTLR3_MALLOC((size_t)size);

	if	(input->data == NULL)
	{
		return		NULL;
	}

	// Structure was allocated correctly, now we can install the pointer and set the size.
	//
	ANTLR3_MEMMOVE(input->data, (const void *)inString, size);
	input->sizeBuf  = size;

	// Call the common 8 bit ASCII input stream handler
	// initializer type thingy doobry function.
	//
	antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);


	input->istream->streamName	= input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)"-memory-" : name);
	input->fileName				= input->istream->streamName;

	return  input;
}
pANTLR3_INPUT_STREAM MemoryMappedInputBufferNew(const std::string& filename) {
  // Pointer to the input stream we are going to create
  //
  pANTLR3_INPUT_STREAM input;
  ANTLR3_UINT32 status;

  // Allocate memory for the input stream structure
  //
  input = (pANTLR3_INPUT_STREAM) ANTLR3_CALLOC(1, sizeof(ANTLR3_INPUT_STREAM));

  if(input == NULL) {
    return NULL;
  }

  // Structure was allocated correctly, now we can read the file.
  //
  status = MemoryMapFile(input, filename);

  // Call the common 8 bit ASCII input stream handler
  // Initializer type thingy doobry function.
  //
#ifdef CVC4_ANTLR3_OLD_INPUT_STREAM
  antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);
#else /* CVC4_ANTLR3_OLD_INPUT_STREAM */
  antlr38BitSetupStream(input);
#endif /* CVC4_ANTLR3_OLD_INPUT_STREAM */

  // Now we can set up the file name
  //
  input->istream->streamName
      = input->strFactory->newStr(input->strFactory,
                                  (uint8_t*) filename.c_str());
  input->fileName = input->istream->streamName;
  input->free = UnmapFile;

  if(status != ANTLR3_SUCCESS) {
    input->close(input);
    return NULL;
  }

  return input;
}
Example #6
0
/** \brief Use the contents of an operating system file as the input
 *         for an input stream.
 *
 * \param fileName Name of operating system file to read.
 * \return
 *	- Pointer to new input stream context upon success
 *	- One of the ANTLR3_ERR_ defines on error.
 */
ANTLR3_API pANTLR3_INPUT_STREAM
antlr3AsciiFileStreamNew(pANTLR3_UINT8 fileName)
{
    /* Pointer to the input stream we are going to create
     */
    pANTLR3_INPUT_STREAM    input;
    ANTLR3_UINT64	    status;

    /* Allocate memory for the input stream structure
     */
    input   = (pANTLR3_INPUT_STREAM)
		    ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM));

    if	(input == NULL)
    {
	return	(pANTLR3_INPUT_STREAM) ANTLR3_ERR_NOMEM;
    }

    input->fileName  = ANTLR3_STRDUP(fileName);

    /* Structure was allocated correctly, now we can read the file.
     */
    status  = antlr3readAscii(input);

    if	(status != ANTLR3_SUCCESS)
    {
	ANTLR3_FREE(input->fileName);
	ANTLR3_FREE(input);

	return	ANTLR3_FUNC_PTR(status);
    }

    /* Call the common 8 bit ASCII input stream handler
     * intializer type thingy doobry function.
     */
    antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);



    return  input;
}