[051bc69a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @file Coordinate span
|
---|
| 30 | *
|
---|
| 31 | * Captures the origin (input object, starting and ending line number and
|
---|
| 32 | * columnt number) of a code fragment.
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <assert.h>
|
---|
| 36 | #include <stdio.h>
|
---|
| 37 | #include <stdlib.h>
|
---|
| 38 | #include "mytypes.h"
|
---|
| 39 |
|
---|
| 40 | #include "cspan.h"
|
---|
| 41 |
|
---|
| 42 | /** Allocate new coordinate span.
|
---|
| 43 | *
|
---|
| 44 | * @param input Input object.
|
---|
| 45 | * @param line Starting line number.
|
---|
| 46 | * @param col0 Starting column number.
|
---|
| 47 | * @param line Ending number (inclusive).
|
---|
| 48 | * @param col1 Ending column number (inclusive).
|
---|
| 49 | *
|
---|
| 50 | * @return New coordinate span.
|
---|
| 51 | */
|
---|
| 52 | cspan_t *cspan_new(input_t *input, int line0, int col0, int line1, int col1)
|
---|
| 53 | {
|
---|
| 54 | cspan_t *cspan;
|
---|
| 55 |
|
---|
| 56 | cspan = calloc(1, sizeof(cspan_t));
|
---|
| 57 | if (cspan == NULL) {
|
---|
| 58 | printf("Memory allocation failed.\n");
|
---|
| 59 | exit(1);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | cspan->input = input;
|
---|
| 63 | cspan->line0 = line0;
|
---|
| 64 | cspan->col0 = col0;
|
---|
| 65 | cspan->line1 = line1;
|
---|
| 66 | cspan->col1 = col1;
|
---|
| 67 |
|
---|
| 68 | return cspan;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /** Create a merged coordinate span.
|
---|
| 72 | *
|
---|
| 73 | * Creates the smalles cspan covering cpans @a a and @a b. Both spans
|
---|
| 74 | * must be from the same input object and @a a must start earlier
|
---|
| 75 | * than @a b terminates.
|
---|
| 76 | *
|
---|
| 77 | * @param a First coordinate span
|
---|
| 78 | * @param b Second coordinate span
|
---|
| 79 | * @return New coordinate span.
|
---|
| 80 | */
|
---|
| 81 | cspan_t *cspan_merge(cspan_t *a, cspan_t *b)
|
---|
| 82 | {
|
---|
| 83 | assert(a != NULL);
|
---|
| 84 | assert(b != NULL);
|
---|
| 85 | assert(a->input == b->input);
|
---|
| 86 |
|
---|
| 87 | return cspan_new(a->input, a->line0, a->col0, b->line1, b->col1);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /** Print coordinate span.
|
---|
| 91 | *
|
---|
| 92 | * @param cspan Coordinate span
|
---|
| 93 | */
|
---|
| 94 | void cspan_print(cspan_t *cspan)
|
---|
| 95 | {
|
---|
| 96 | printf("%s:", cspan->input->name);
|
---|
| 97 |
|
---|
| 98 | if (cspan->line0 != cspan->line1) {
|
---|
| 99 | printf("%d:%d-%d:%d", cspan->line0, cspan->col0, cspan->line1,
|
---|
| 100 | cspan->col1);
|
---|
| 101 | } else {
|
---|
| 102 | printf("%d:%d-%d", cspan->line0, cspan->col0, cspan->col1);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|